Results 1 to 6 of 6

Thread: Sqlite multipule row insert question

  1. #1
    Join Date
    Mar 2009
    Location
    Nashville Tn.
    Posts
    53
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Sqlite multipule row insert question

    I am trying to insert several rows into sqlite database, i have searched this fourm and the net but still can not get this to work, it just inserts the first row correctly but nothing is inserted after the first row. I have verified the query is updated to the correct values on each loop. can you help me?

    Qt Code:
    1. templatedb.transaction();
    2. while(!db->eof()){
    3.  
    4. theQuery="";
    5. theQuery ="INSERT INTO reporttemplates (reportname ,itemtag,itemposx,itemposy,scale,angle,zvalue,data,originalrect,currentrect,";
    6. theQuery =theQuery+"type,fontindex,color,fntsize,isbold,isitalic,isunderlined,pagenum,showonallpages,opacity)";
    7. theQuery =theQuery+ " VALUES('"+ui->lineEdit->text()+"','"+db->getCell(db->currentRow(),0)+"','"+db->getCell(db->currentRow(),1)+"',";
    8. theQuery =theQuery+"'"+db->getCell(db->currentRow(),2)+"','"+db->getCell(db->currentRow(),3)+"','"+db->getCell(db->currentRow(),4)+"',";
    9. theQuery =theQuery+"'"+db->getCell(db->currentRow(),5)+"','"+db->getCell(db->currentRow(),6)+"','"+db->getCell(db->currentRow(),7)+"',";
    10. theQuery =theQuery+"'"+db->getCell(db->currentRow(),8)+"','"+db->getCell(db->currentRow(),9)+"','"+db->getCell(db->currentRow(),10)+"',";
    11. theQuery =theQuery+"'"+db->getCell(db->currentRow(),11)+"','"+db->getCell(db->currentRow(),12)+"','"+db->getCell(db->currentRow(),13)+"',";
    12. theQuery =theQuery+"'"+db->getCell(db->currentRow(),14)+"','"+db->getCell(db->currentRow(),15)+"','"+db->getCell(db->currentRow(),16)+"',";
    13. theQuery =theQuery+"'"+db->getCell(db->currentRow(),17)+"','"+db->getCell(db->currentRow(),18)+"')" ;
    14. templatedb.exec(theQuery);
    15.  
    16.  
    17.  
    18. db->next();
    19. int row =db->currentRow();
    20. }
    21.  
    22. templatedb.exec("COMMIT");
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sqlite multipule row insert question

    What does QSqlDatabase::lastError () tell you about each attempted insert?

    With the code I can see you will need to be wary of single quotes appearing in the source data because it will break the SQL. Reportname and data look like the obvious candidates but everything is quoted in your SQL, so anything could be. If you have this happening then perhaps you should consider QSqlQuery::prepare() and QSqlQuery::bindValue() rather than directly accessing the database driver with a constructed string.

    You would typically use QSqlDatabase::commit() to commit your transaction.

  3. #3
    Join Date
    Mar 2009
    Location
    Nashville Tn.
    Posts
    53
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sqlite multipule row insert question

    ChrisW,

    thanks for your answer, I used the QDebug (i should have thought of that )

    it gives error 19 unable to fetch row , constraint failed.


    i am not sure if that means it can not get info from sqlite or the "db" database.

  4. #4
    Join Date
    Mar 2009
    Location
    Nashville Tn.
    Posts
    53
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy Re: Sqlite multipule row insert question

    I have changed to bindvalue ,still same problem.

    the first record is inserted into the sqlite database (templatedb) correctly but no record is inserted after that. i have found that the query goes inactive after the first successful query.exec().

    I have found several post about this behavior but i can find NO solution. Does ANYBODY know what the problem is?


    Qt Code:
    1. int x=0;
    2. int numrecords= db.recordcount;
    3.  
    4. while(x<=numrecords){
    5. QString theQuery;
    6. QSqlQuery query(theQuery, templatedb) ;
    7.  
    8.  
    9. theQuery="";
    10. theQuery ="INSERT INTO reporttemplates (reportname,itemtag,itemposx,itemposy,scale,angle,zvalue,data,originalrect,currentrect,";
    11. theQuery =theQuery+"type,fontindex,color,fntsize,isbold,isitalic,isunderlined,pagenum,showonallpages,opacity)";
    12. theQuery =theQuery+ " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    13.  
    14. query.prepare(theQuery);
    15.  
    16. query.bindValue(0,ui->lineEdit->text());
    17. query.bindValue(1,db->getCell(row,0));
    18. query.bindValue(2,db->getCell(row,1));
    19. query.bindValue(3,db->getCell(row,2));
    20. query.bindValue(4,db->getCell(row,3));
    21. query.bindValue(5,db->getCell(row,4));
    22. query.bindValue(6,db->getCell(row,5));
    23. query.bindValue(7,db->getCell(row,6));
    24. query.bindValue(8,db->getCell(row,7));
    25. query.bindValue(9,db->getCell(row,8));
    26. query.bindValue(10,db->getCell(row,9));
    27. query.bindValue(11,db->getCell(row,10));
    28. query.bindValue(12,db->getCell(row,11));
    29. query.bindValue(13,db->getCell(row,12));
    30. query.bindValue(14,db->getCell(row,13));
    31. query.bindValue(15,db->getCell(row,14));
    32. query.bindValue(16,db->getCell(row,15));
    33. query.bindValue(17,db->getCell(row,16));
    34. query.bindValue(18,db->getCell(row,17));
    35. query.bindValue(19,db->getCell(row,18));
    36.  
    37.  
    38. qDebug() << query.exec(); <- works correctly one time , next returns false.
    39. query.next(); <-try to move to last row
    40.  
    41. qDebug() << query.lastError(); <-2nd time returns QSqlError(19,"unable to fetch row", "constraint failed")
    42. x++
    43. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sqlite multipule row insert question

    Line 39 should be
    Qt Code:
    1. db.next()
    To copy to clipboard, switch view to plain text mode 
    like your original code shouldn't it?

    The message regarding violation of a constraint can only be coming from the insert. Duplicate primary key value would be the first place to look although there can be other constraints defined on the reporttemplates table. It's not clear which (if any) column is a unique identifier for a row. If you are not inserting a value into any primary key column, and it is not an auto-increment type field, then the second row insert will have the same id as the first (e.g. NULL, 0 or empty) and fail.

  6. The following user says thank you to ChrisW67 for this useful post:

    rogerholmes (31st December 2009)

  7. #6
    Join Date
    Mar 2009
    Location
    Nashville Tn.
    Posts
    53
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sqlite multipule row insert question

    THANK YOU CHRIS !!!

    The primary key was THE issue.

Similar Threads

  1. Insert unicode in SQlite
    By Kastagne in forum Qt Programming
    Replies: 3
    Last Post: 11th October 2011, 14:07
  2. SQLite sometimes doens't INSERT into database
    By cevou in forum Qt Programming
    Replies: 5
    Last Post: 30th October 2009, 08:10
  3. SQLite beginner question...
    By theMac in forum Newbie
    Replies: 3
    Last Post: 2nd September 2009, 00:01
  4. Replies: 0
    Last Post: 4th December 2008, 05:48
  5. Bulk insert into SQLite
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2007, 03:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.