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. [SOLVED]Why QSqlQuery faild after first use ::prepare and ::exec?

[SOLVED]Why QSqlQuery faild after first use ::prepare and ::exec?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.8k Views 1 Watching
  • 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.
  • P Offline
    P Offline
    pplly
    wrote on last edited by
    #1

    @#include <QtCore/QCoreApplication>

    #include <qsqldatabase.h>
    #include <qsqlquery.h>
    #include <qvariant.h>
    #include <iostream>

    using namespace std;

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QSqlDatabase sqlDatabase=QSqlDatabase::addDatabase("QSQLITE");
    sqlDatabase.setDatabaseName("my_database.db");
    sqlDatabase.open();
    QSqlQuery query;
    query.exec("create table test(id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar)");
    for(int i=0;i<3;i++)
    {
    cout<<query.prepare("insert into test values(:id,:name)")<<endl;
    query.bindValue(":id",NULL);
    query.bindValue(":name","Name");
    cout<<query.exec()<<endl;
    //query.exec("insert into test values(NULL,'Name')");
    }
    query.exec("select * from test");
    while(query.next())
    {
    cout<<query.value(0).toInt()<<endl;
    cout<<query.value(1).toString().toStdString()<<endl;
    }
    return a.exec();
    }
    @

    it can only insert once, after the first insertion, all query.exec() returns fail;

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Hi!
      Change your insertion as:
      @for(int i=0;i<3;i++)
      {
      cout<<query.prepare("insert into test (name)values(:name)")<<endl;
      query.bindValue(":name","Name");
      cout<<query.exec()<<endl;
      //query.exec("insert into test values(NULL,'Name')");
      }@
      id field in your table is auto increment primary key, it means sqlite will care about this field.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mmallad
        wrote on last edited by
        #3

        Please get actual error .
        @
        query.lastError().text();
        @

        Dpak Malla

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pplly
          wrote on last edited by
          #4

          Thx O(∩_∩)O

          [quote author="qxoz" date="1362130762"]Hi!
          Change your insertion as:
          @for(int i=0;i<3;i++)
          {
          cout<<query.prepare("insert into test (name)values(:name)")<<endl;
          query.bindValue(":name","Name");
          cout<<query.exec()<<endl;
          //query.exec("insert into test values(NULL,'Name')");
          }@
          id field in your table is auto increment primary key, it means sqlite will care about this field.[/quote]

          1 Reply Last reply
          0

          • Login

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