Results 1 to 14 of 14

Thread: Problem of closing database

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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

    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.

  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: 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).

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

    cooper (3rd March 2011)

  4. #3
    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.