Results 1 to 14 of 14

Thread: Problem of closing database

  1. #1
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Problem of closing database

    Hi everyone,

    My small app has a button to start recording data into a database. A timer event happened every 1 second. My program write data to a database every second after the 'start' button has been clicked. A 'stop' button to stop recording data.

    my problem is that after I click 'stop' button, and click 'start' again, i got a warning message from Qt creator:

    "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."

    I had a look the 'QSqlDatabase' class. the message indicates that a query on the database connection is still opened.

    can anyone tell me how to stop the query in my program, please?
    Thanks in advance.
    here is part of my code:

    Qt Code:
    1. void cycleScrDialog::on_pushButton_clicked()
    2. {
    3. // create new database
    4. QDate sDate = QDate::currentDate();
    5. QString dbName;
    6. dbName = QString(QApplication::applicationDirPath()).append("/I-" + sDate.toString("MMyyyy") + ".db");
    7. m4db = QSqlDatabase::addDatabase("QSQLITE");
    8. m4db.setDatabaseName(dbName);
    9.  
    10. if (!QFile::exists(dbName))
    11. {
    12. mydb.open();
    13. QSqlQuery query;
    14. query.exec("create table c1(ID int primary key unique, "
    15. "StartTime QString, StopTime QString)");
    16. mydb.close();
    17. }
    18.  
    19. if (!mydb.open())
    20. {
    21. QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while "
    22. "opening the connection: ") + m4db.lastError().text());
    23. return;
    24. }
    25. else
    26. {
    27. QSqlQuery query;
    28. query.exec("PRAGMA synchronous = OFF");
    29. model = new QSqlTableModel(this);
    30. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    31.  
    32. model->setTable("c1");
    33. model->select();
    34. model->insertRow(0);
    35. model->setData(model->index(0, 1), String(qdatetime.toString(Qt::ISODate)));
    36. model->setData(model->index(0, 2), String(qdatetime.toString(Qt::ISODate)));
    37. model->submitAll();
    38. }
    39.  
    40. saveIni();
    41. model->setTable("c1");
    42. model->filter();
    43. model->select();
    44. }
    45.  
    46. void cycleScrDialog::timer1000Event()
    47. {
    48. model->setTable("c1");
    49. model->select();
    50. model->insertRow(0);
    51. model->setData(model->index(0, 1), String(qdatetime.toString(Qt::ISODate)));
    52. model->setData(model->index(0, 2), String(qdatetime.toString(Qt::ISODate)));
    53. model->submitAll();
    54. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem of closing database

    Your example is incomplete, but I guess you have to delete model first.

  3. #3
    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: Problem of closing database

    All objects that depend on the database connection must have been destroyed or disconnected (in some cases a clear() method exists). If you don't use the default connection name then you can simply call removeDatabase() with your known connection name. If you want to remove the default connection:
    Qt Code:
    1. QString conName;
    2. {
    3. QSqlDatabase db = QSqlDatabase::database();
    4. conName = db.connectionName();
    5. }
    6. QSqlDatabase::removeDatabase(conName);
    To copy to clipboard, switch view to plain text mode 
    The braces ensure that db is destroyed before the removeDatabase() call otherwise the warning persists.

  4. #4
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem of closing database

    Note:
    in my code, the database “m4db" should be "mydb", sorry about my typing mistake.

    Hi Lykurg,

    Which part of my code is incomplete?
    if you mentioned about closing database, i add it as below.

    To delete model, do i use model->clear() command?


    Hi ChrisW67,

    I close my database when i click "stop" button.

    Qt Code:
    1. // stop button
    2. void cycleScrDialog::on_pushButton_2_clicked()
    3. {
    4. mydb.close();
    5. QSqlDatabase::removeDatabase("QSQLITE");
    6. }
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot

  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: Problem of closing database

    Quote Originally Posted by cooper View Post
    To delete model, do i use model->clear() command?
    No,
    Qt Code:
    1. delete model;
    2. model = 0; // for safety if you intend the container object to hang around and reuse the model pointer.
    To copy to clipboard, switch view to plain text mode 
    I close my database when i click "stop" button.

    Qt Code:
    1. // stop button
    2. void cycleScrDialog::on_pushButton_2_clicked()
    3. {
    4. mydb.close();
    5. QSqlDatabase::removeDatabase("QSQLITE");
    6. }
    To copy to clipboard, switch view to plain text mode 

    Thanks a lot
    mydb, closed or not, is still a reference to the database and you will be receiving the warning. Don't hold the reference to the database as a member variable, just pick it up in local variables when required:
    Qt Code:
    1. void someCoolMethod()
    2. {
    3. QSqlDatabase db = QSqlDatabase::database();
    4. ... do stuff with db
    5. }
    To copy to clipboard, switch view to plain text mode 
    and you never have references hanging around. Once you correct the argument to removeDatabase() (it is the connection name not the database type) then your close routine should be warning free.

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

    cooper (1st March 2011)

  7. #6
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem of closing database

    Thanks ChrisW67.

    Can i have another question please?
    as you see in my code:
    Qt Code:
    1. QSqlQuery query;
    2. query.exec("PRAGMA synchronous = OFF");
    3. model = new QSqlTableModel(this);
    4. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    5.  
    6. model->setTable("c1");
    7. model->select();
    8. model->insertRow(0);
    9. model->setData(model->index(0, 1), String(qdatetime.toString(Qt::ISODate)));
    10. model->setData(model->index(0, 2), String(qdatetime.toString(Qt::ISODate)));
    11. model->setData(model->index(0, 3), 1); // original value is 1 in int
    12. model->submitAll();
    To copy to clipboard, switch view to plain text mode 

    when i click 'stop' button, i want to change the value in last row. here is my code:
    Qt Code:
    1. model->setTable("c1");
    2. model->select();
    3. int row = model->rowCount();
    4. model->setData(model->index(row, 3), 55); // change the value to be 55 in int
    5. model->sumitAll();
    To copy to clipboard, switch view to plain text mode 

    however, after i stop my program and take a look database, the last value in (row, 3) has not been changed.

    can you point out where i did wrong please?
    I really appreciate your help.

  8. #7
    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: Problem of closing database

    Firstly, turning of synchronous writing has data integrity risks that you should be aware of; http://www.sqlite.org/pragma.html#pragma_synchronous

    There is no row rowCount() to update in your second snippet. The last existing row in the model is rowCount()-1 (if there are any rows at all).

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

    cooper (1st March 2011)

  10. #8
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem of closing database

    Firstly, turning of synchronous writing has data integrity risks that you should be aware of
    Hi ChristW67, Thanks for you suggestion. The reason why i turn off the sync is that i am writing data (10 different records) to database every second. I am not sure if the Normal (1) Sync is fast enough to perform what i need.

    There is no row rowCount() to update in your second snippet. The last existing row in the model is rowCount()-1
    yes, you are right, i did not realize that.
    However, when i tried the following code, it wrote the data to all my 9th column. but i only want to change the value at the last row. where i missed?
    Qt Code:
    1. model->setTable("c1");
    2. model->select();
    3. int row = model->rowCount();
    4. model->setData(model->index((row-1), 9), 55); // i only want to change the value at the last row to be 55 in int
    5. model->sumitAll();
    To copy to clipboard, switch view to plain text mode 

    Last edited by cooper; 1st March 2011 at 20:42.

  11. #9
    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: Problem of closing database

    The code you show only updates a single row. When are you calling it?

  12. #10
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem of closing database

    I call this code when i click 'stop' button.

    i debugged my program and put a stop mark in front of
    Qt Code:
    1. model->setData(model->index((row-1), 9), 55);
    To copy to clipboard, switch view to plain text mode 
    it only been through this code once till i close my program.
    I have checked through all this program, this is the only place i set data to be 55.

    strange!!!

    EDIT:

    i changed my code a little bit to set last 2nd data to be 55555,
    Qt Code:
    1. model->setData(model->index((row-2), 9), 55555);
    To copy to clipboard, switch view to plain text mode 

    it changed all my 9th column to 55555 not just the last 2nd row.
    my head full of ????? mark.
    Last edited by cooper; 1st March 2011 at 23:10.

  13. #11
    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: Problem of closing database

    If you do not set the value at all what is in the column?
    Does the table have a primary key?
    Is data flowing into the table asynchronously or is access single-threaded?
    Comment out the PRAGMA and see if the behaviour changes.

  14. #12
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem of closing database

    Hi ChrisW67, thanks for your suggestion.

    Comment out the PRAGMA and see if the behaviour changes.
    it does not make any different.

    Does the table have a primary key?
    yes, it has a primary key. here is my code
    Qt Code:
    1. QDate sDate = QDate::currentDate();
    2. QString dbName;
    3. dbName = QString(QApplication::applicationDirPath()).append("/db/I-" + sDate.toString("MMyyyy") + ".db");
    4. mydb = QSqlDatabase::addDatabase("QSQLITE");
    5. mydb.setDatabaseName(dbName);
    6.  
    7. if (!QFile::exists(dbName))
    8. {
    9. mydb.open();
    10. QSqlQuery query;
    11. query.exec("create table cycleParams (ID int primary key unique, "
    12. "CNo int unique, DNo int, CName QString, "
    13. "TSet int, TiSet int, DSet int, STime QString,"
    14. "PTime QString, CycleTime QString, Status int, Spare QByteArray)");
    15.  
    16. mydb.close();
    17. }
    To copy to clipboard, switch view to plain text mode 

    If you do not set the value at all what is in the column?
    ok, i modified my code a little bit. and debugged through my program.
    i did not initial two parameters at the beginning:
    Qt Code:
    1. QSqlQuery query;
    2. //query.exec("PRAGMA synchronous = OFF");
    3. model = new QSqlTableModel(this);
    4. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    5.  
    6. model->setTable("c1");
    7. model->select();
    8. model->insertRow(0);
    9. model->setData(model->index(0, 1), gCNumber);
    10. model->setData(model->index(0, 2), gDNumber);
    11. model->setData(model->index(0, 3), gCName);
    12. model->setData(model->index(0, 4), gSTemp);
    13. model->setData(model->index(0, 5), gSTime);
    14. model->setData(model->index(0, 6), gDTime);
    15. model->setData(model->index(0, 7), QString(qdatetime.toString(Qt::ISODate)));
    16. model->setData(model->index(0, 8), QString(qdatetime.toString(Qt::ISODate)));
    17. //model->setData(model->index(0, 9), 0);
    18. //model->setData(model->index(0, 10), 0);
    19. model->setData(model->index(0, 11), "");
    20. model->submitAll();
    To copy to clipboard, switch view to plain text mode 

    then, i got a blank value
    d1.png

    after i submitted values:
    Qt Code:
    1. model->setTable("c1");
    2. model->select();
    3. int row = model->rowCount() - 1;
    4. model->setData(model->index(row, 10), gErrorCode);
    5. model->setData(model->index(row, 9), gTotalCycleTime);
    6. model->submitAll();
    To copy to clipboard, switch view to plain text mode 
    all past records in the same column have been changed.
    d2.png
    Last edited by cooper; 2nd March 2011 at 23:58.

  15. #13
    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: Problem of closing database

    Quote Originally Posted by cooper View Post
    yes, it has a primary key. here is my code
    Qt Code:
    1. if (!QFile::exists(dbName))
    2. {
    3. mydb.open();
    4. QSqlQuery query;
    5. query.exec("create table cycleParams (ID int primary key unique, "
    6. "CNo int unique, DNo int, CName QString, "
    7. "TSet int, TiSet int, DSet int, STime QString,"
    8. "PTime QString, CycleTime QString, Status int, Spare QByteArray)");
    9.  
    10. mydb.close();
    11. }
    To copy to clipboard, switch view to plain text mode 
    The data types of the columns in your Sqlite database are not Qt datatypes (i.e. QString, QByteArray). This is only working because Sqlite does not enforce data types. See http://www.sqlite.org/datatype3.html.

    You are allowing NULL into the column you have labelled " int primary key unique". This is not good given that you then insertRow() and do not provide a value for the ID column (column 0). Your ID column will be NULL. If you were expecting Sqlite to provide a unique integer key then the table should be defined:
    Qt Code:
    1. create table cycleParams (
    2. ID INTEGER PRIMARY KEY, -- literally this (case insensitive)
    3. CNo int unique, DNo int,
    4. CName varchar(100),
    5. TSet int, TiSet int, DSet int, STime varchar(20),
    6. PTime varchar(20), CycleTime varchar(20), Status int, Spare blob)
    To copy to clipboard, switch view to plain text mode 
    If the ID number must be montonically increasing then use "INTEGER PRIMARY KEY AUTOINCREMENT". See http://www.sqlite.org/lang_createtable.html#rowid and http://www.sqlite.org/autoinc.html

    Fixing the key will go some way to solving this problem.

    after i submitted values:
    Qt Code:
    1. model->setTable("c1");
    2. model->select();
    3. int row = model->rowCount() - 1;
    4. model->setData(model->index(row, 10), gErrorCode);
    5. model->setData(model->index(row, 9), gTotalCycleTime);
    6. model->submitAll();
    To copy to clipboard, switch view to plain text mode 
    all past records in the same column have been changed.
    I assume c1 and cycleParams are the same table.
    Why do you reset the model table and reselect here rather than just using the same, already established, model?
    Since I expect there to be many rows in this table you should make sure that all have been fetched into the model:
    Qt Code:
    1. model->select();
    2. while (model->canFetchMore())
    3. model->fetchMore();
    4. ...
    To copy to clipboard, switch view to plain text mode 

    An alternate approach would be to simply use a query to update the 'last' row (by whatever definition last is judged).

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

    cooper (3rd March 2011)

  17. #14
    Join Date
    Jun 2009
    Location
    AKL | New Zealand
    Posts
    62
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem of closing database

    Solved.
    After i replace primary key id to be ID INTEGER PRIMARY KEY, it works perfectly.

    Thanks chrisw67. I really appreciate the time you have taken to point me to the right direction.

Similar Threads

  1. Database closing warnings
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 9th December 2010, 00:31
  2. Replies: 5
    Last Post: 21st April 2010, 21:36
  3. Problem regarding database in Qt
    By sudheer168 in forum Qt Programming
    Replies: 4
    Last Post: 6th January 2009, 11:04
  4. Problem with closing the application
    By macbeth in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2008, 15:02
  5. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49

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
  •  
Qt is a trademark of The Qt Company.