Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sqlite nested select
Forum Updated to NodeBB v4.3 + New Features

Sqlite nested select

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlite3nesselect
4 Posts 3 Posters 398 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GeorgiosKoropoulis
    wrote on 19 Apr 2024, 16:18 last edited by
    #1

    Hi ,
    I am using sqlite to hold a table as such :
    CREATE TABLE IF NOT EXISTS table_name (
    id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL
    );

    table is created ok using QSqlQuery prepare() followed by exec().
    My problem arises when I try to add an item turning following statement:

    const auto addanItem = QLatin1String(R"(INSERT INTO table_name (id,title) VALUES (:id,: title))");
    QString queryStr (addanItem);

    QSqlQuery queryAdd;
    queryAdd.prepare(queryStr); // this is succesful

    queryAdd.bindValue(":id", "(SELECT id FROM table_name WHERE title = 'someNewTitle')");
    queryAdd.bindValue(":title", "someNewTitle");

    bool success = queryAdd.exec(); // this fails with "Unable to fetch row", "datatype mismatch"...

    So question is : Can we use nested queries such as (in SQLITE terminology )
    "INSERT OR IGNORE INTO table_name (id,title) VALUES ((SELECT id FROM table_name WHERE title='someNewTitle'),'someNewTitle)';

    I tested it with plain SQLITE commands and seems working OK

    C 1 Reply Last reply 19 Apr 2024, 16:48
    0
    • G GeorgiosKoropoulis
      19 Apr 2024, 16:18

      Hi ,
      I am using sqlite to hold a table as such :
      CREATE TABLE IF NOT EXISTS table_name (
      id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT,
      title TEXT NOT NULL
      );

      table is created ok using QSqlQuery prepare() followed by exec().
      My problem arises when I try to add an item turning following statement:

      const auto addanItem = QLatin1String(R"(INSERT INTO table_name (id,title) VALUES (:id,: title))");
      QString queryStr (addanItem);

      QSqlQuery queryAdd;
      queryAdd.prepare(queryStr); // this is succesful

      queryAdd.bindValue(":id", "(SELECT id FROM table_name WHERE title = 'someNewTitle')");
      queryAdd.bindValue(":title", "someNewTitle");

      bool success = queryAdd.exec(); // this fails with "Unable to fetch row", "datatype mismatch"...

      So question is : Can we use nested queries such as (in SQLITE terminology )
      "INSERT OR IGNORE INTO table_name (id,title) VALUES ((SELECT id FROM table_name WHERE title='someNewTitle'),'someNewTitle)';

      I tested it with plain SQLITE commands and seems working OK

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 19 Apr 2024, 16:48 last edited by
      #2

      @GeorgiosKoropoulis said in Sqlite nested select:

      queryAdd.bindValue(":id", "(SELECT id FROM table_name WHERE title = 'someNewTitle')");

      How should this work? You can only bind values, not subselects.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      G 1 Reply Last reply 20 Apr 2024, 08:42
      2
      • C Christian Ehrlicher
        19 Apr 2024, 16:48

        @GeorgiosKoropoulis said in Sqlite nested select:

        queryAdd.bindValue(":id", "(SELECT id FROM table_name WHERE title = 'someNewTitle')");

        How should this work? You can only bind values, not subselects.

        G Offline
        G Offline
        GeorgiosKoropoulis
        wrote on 20 Apr 2024, 08:42 last edited by
        #3

        @Christian-Ehrlicher said in Sqlite nested select:

        ly bind values, not subselects.

        Many thanks! That s exactly what I was asking

        J 1 Reply Last reply 20 Apr 2024, 08:49
        0
        • G GeorgiosKoropoulis
          20 Apr 2024, 08:42

          @Christian-Ehrlicher said in Sqlite nested select:

          ly bind values, not subselects.

          Many thanks! That s exactly what I was asking

          J Online
          J Online
          JonB
          wrote on 20 Apr 2024, 08:49 last edited by JonB
          #4

          @GeorgiosKoropoulis
          If you want to use whatever SQLite constructs in queries you have, like INSERT OR IGNORE INTO ... or SELECT ... FROM/VALUES (SELECT ... FROM), you can but you have to build the text of the query yourself. In this case you may find that binding does not work (that is supported by the driver, so depends what that allows for binding), but you can build that yourself into the text of the query without using binding. Just beware of correct syntax (e.g. quoting) and SQL injection.

          1 Reply Last reply
          1

          4/4

          20 Apr 2024, 08:49

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved