Results 1 to 7 of 7

Thread: QSQLITE database changes not showing up in database

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSQLITE database changes not showing up in database

    I have two databases im working with. They are both for different form interfaces. One works but the other database doesn't show any changes when I make an update and I get no errors in my code so it's quite difficult to troubleshoot. Here is a listing of my db setup and where I update in both:

    The working db:
    Qt Code:
    1. #include <QtSql>
    2.  
    3. #define Path_to_DB "/media/.../dbpath"
    4.  
    5. .....
    6.  
    7. {
    8. ui->setupUi(this);
    9. db = QSqlDatabase::addDatabase("QSQLITE");
    10. db.setDatabaseName(Path_to_DB);
    11. QFileInfo checkFile(Path_to_DB);
    12.  
    13. if(checkFile.isFile())
    14. {
    15. if(db.open())
    16. {
    17. qDebug() << "Connected to database file";
    18. }
    19. }else{
    20. qDebug() << "Database file not found";
    21. }
    22. }
    23.  
    24. adminDialog::~adminDialog()
    25. {
    26. delete ui;
    27. qDebug() << "Closing connection to Database file on exit...";
    28. db.close();
    29. }
    30.  
    31. double count;
    32.  
    33. void adminDialog::on_pushButton_3_clicked()
    34. {
    35. count++;
    36. QString str = ui->lineEdit->text();
    37. ui->count->display(count);
    38.  
    39. qDebug() << str;
    40.  
    41. if(!db.isOpen()){
    42. qDebug() << "No connection to db";
    43. return;
    44. }
    45.  
    46. QSqlQuery query;
    47.  
    48. QString qry = QString("INSERT INTO customer (name, size1, size2, size3) VALUES ('%1', NULL, NULL, NULL)").arg(str);
    49.  
    50. query.prepare(qry);
    51. if(query.exec())
    52. {
    53. qDebug() << "Update Complete";
    54. }
    55. else
    56. {
    57. qDebug() << query.lastError();
    58. }
    59. }
    To copy to clipboard, switch view to plain text mode 

    db thats not working:

    Qt Code:
    1. #include <QtSql>
    2.  
    3. #define Path_to_DB "/media/.../db2path"
    4. ......
    5.  
    6. ui->setupUi(this);
    7. db2 = QSqlDatabase::addDatabase("QSQLITE");
    8. db2.setDatabaseName(Path_to_DB);
    9. QFileInfo checkFile(Path_to_DB);
    10.  
    11. if(checkFile.isFile())
    12. {
    13. if(db2.open())
    14. {
    15. qDebug() << "Connected to database file";
    16. }
    17. }else{
    18. qDebug() << "Database file not found";
    19. }
    20. }
    21.  
    22. void demo::on_pushButton_clicked()
    23. {
    24. if(!db2.isOpen()){
    25. qDebug() << "No connection to db";
    26. return;
    27. }
    28. ........
    29.  
    30. QSqlQuery query;
    31. switch (md_pref)
    32. {
    33. case 1:
    34. query.prepare("UPDATE customer SET size1 = size1 + 1 WHERE name = 'Gary' ");
    35. if(query.exec())
    36. {
    37. qDebug() << "Updated Gary";
    38. }
    39. else
    40. {
    41. qDebug() << query.lastError();
    42. }
    43. break;
    44. .............
    45.  
    46. }
    To copy to clipboard, switch view to plain text mode 

    I'm working on a Linux Kubuntu 12.10 system. I appreciate all help on this thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSQLITE database changes not showing up in database

    What does QSqlQuery::exec() return? What does prepare() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSQLITE database changes not showing up in database

    Quote Originally Posted by wysota View Post
    What does QSqlQuery::exec() return? What does prepare() return?
    Well I get no errors when running just like in the working version. The qDebug outputs updated but nothing actually happens.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSQLITE database changes not showing up in database

    And what does QSqlQuery::numRowsAffected() return?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSQLITE database changes not showing up in database

    Quote Originally Posted by wysota View Post
    And what does QSqlQuery::numRowsAffected() return?
    Returns the correct number of rows affected...

    After doing some manual debugging i've seen the following in application output when I change between the windows that connect to the different databases.

    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    Looks like its using the same connection for the two dbs not sure what to actually change though because when I tried changing something it gave me an error saying " Driver not loaded"....
    Last edited by Cyrebo; 14th April 2013 at 22:19.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSQLITE database changes not showing up in database

    If you want help then provide something we can compile and test. Otherwise you're on your own.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSQLITE database changes not showing up in database

    It's working now! I hadn't set the default db value that was being updated to 0 like I did in the one that's working lol...smh

    P.S. Found that out while I was preparing to upload all my code.
    Last edited by Cyrebo; 14th April 2013 at 23:27.

Similar Threads

  1. Replies: 3
    Last Post: 24th February 2013, 04:20
  2. QSQLITE database, delete rows, frozen columns
    By carvi in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2011, 02:11
  3. QSqlite, multiple connections to in memory database.
    By adzajac in forum Qt Programming
    Replies: 9
    Last Post: 10th March 2010, 22:35
  4. QSQLITE database can't exec queries
    By mcwar in forum Qt Programming
    Replies: 5
    Last Post: 12th January 2010, 23:47
  5. QSqlite database lock + Delegate + QSqlQueryModel
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2009, 11:52

Tags for this Thread

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.