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
QtWS25 Last Chance

Sqlite nested select

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlite3nesselect
4 Posts 3 Posters 370 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 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

    Christian EhrlicherC 1 Reply Last reply
    0
    • G GeorgiosKoropoulis

      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

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      2
      • Christian EhrlicherC Christian Ehrlicher

        @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 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

        JonBJ 1 Reply Last reply
        0
        • G GeorgiosKoropoulis

          @Christian-Ehrlicher said in Sqlite nested select:

          ly bind values, not subselects.

          Many thanks! That s exactly what I was asking

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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

          • Login

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