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 statement CREATE TABLE with binding rowid
QtWS25 Last Chance

SQLite statement CREATE TABLE with binding rowid

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlitedatabaseqt5.5sqltable
2 Posts 2 Posters 3.0k 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.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by Kofr
    #1

    Hello guys,
    I am not a guru of SQL yet. So I come across with a problem:
    I need to execute SQL statement in context of using Qt as follows:
    CREATE TABLE IF NOT EXISTS tasks (taskId INTEGER PRIMARY KEY DEFAULT numeric_literal)
    And I need to bind numeric_literal to Value from C++ code/
    It is not clear for me how to implement this.
    Something like that:

    int taskIdFromCppCode = valueForInitialization;
    QSqlQuery query;
        query.prepare("CREATE TABLE IF NOT EXISTS tasks (taskId INTEGER PRIMARY KEY DEFAULT taskid "
                      "VALUES (:taskid)");
        query.bindValue(":taskid", taskIdFromCppCode );
        query.exec();
    

    Is that right approach or how to fill the table with my own rowids?

    kshegunovK 1 Reply Last reply
    0
    • K Kofr

      Hello guys,
      I am not a guru of SQL yet. So I come across with a problem:
      I need to execute SQL statement in context of using Qt as follows:
      CREATE TABLE IF NOT EXISTS tasks (taskId INTEGER PRIMARY KEY DEFAULT numeric_literal)
      And I need to bind numeric_literal to Value from C++ code/
      It is not clear for me how to implement this.
      Something like that:

      int taskIdFromCppCode = valueForInitialization;
      QSqlQuery query;
          query.prepare("CREATE TABLE IF NOT EXISTS tasks (taskId INTEGER PRIMARY KEY DEFAULT taskid "
                        "VALUES (:taskid)");
          query.bindValue(":taskid", taskIdFromCppCode );
          query.exec();
      

      Is that right approach or how to fill the table with my own rowids?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @Kofr
      Hello,
      Your SQL is incorrect. Create the table and then insert the rows.

      QSqlQuery query("CREATE TABLE tasks (taskId INTEGER, PRIMARY KEY(taskId))");
      if (!query.exec())
          ; //< Handle error - table couldn't be created
      
      query.prepare("INSERT INTO tasks (taskId) VALUES (:id)");
      
      // From here on you can have multiple calls to the same piece of code to insert multiple rows
      query.bindValue(":id", valueForInitialization);
      if (!query.exec())
          ; //< Handle error - can't insert row
      

      Additionally, I advise you to create the table once and for all. Then just fill it up, instead of trying to create it on every row insertion.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      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