Results 1 to 3 of 3

Thread: QSqlite issues

  1. #1
    Join Date
    Sep 2010
    Posts
    7
    Thanks
    1
    Qt products
    Qt4

    Default QSqlite issues

    Hey guys,

    I'm having this trouble in QT - I was wondering if somebody could explain to me what's going on.

    I'm using this code:
    Qt Code:
    1. timingDatabase = QSqlDatabase::addDatabase("QSQLITE", "timing");
    2. timingDatabase.setDatabaseName("../config.db");
    3. if (!timingDatabase.open())
    4. {
    5. qDebug() << timingDatabase.lastError();
    6. }
    7.  
    8. if (!timingDatabase.tables().contains("timing"))
    9. ConstructTimeSettingsTable();
    To copy to clipboard, switch view to plain text mode 

    and also
    Qt Code:
    1. void TimingSettings::ConstructTimeSettingsTable()
    2. {
    3. qDebug() << timingDatabase.tables();
    4. QSqlQuery timeQuery;
    5. timeQuery.exec("CREATE TABLE timing ("
    6. "line1 STRING, "
    7. "line2 STRING, "
    8. "line3 STRING"
    9. ");");
    10. qDebug() << timeQuery.lastError();
    11. qDebug() << "Construct function entered.";
    12. }
    To copy to clipboard, switch view to plain text mode 


    If I am correct - my program should run the Construct...() if the timing table does not exist (i.e. the first time the program is ran).. However, that is not the case - this is the output I am getting from my qDebug everytime I run the program:
    Qt Code:
    1. ()
    2. QSqlError(1, "Unable to execute statement", "table timing already exists")
    3. Construct function entered.
    To copy to clipboard, switch view to plain text mode 

    (1) the first "()" is what timingDatabase.tables() - as in there are no tables in the database, am I right?
    (2) if the timingDatabase.tables() is NULL, why is the timingQuery.exec unable to run because the table already exists..?
    (3) I've also written debugging code to try to write to and from the database - but the code does not do anything.

    If you guys could point me in the right direction of what is wrong with my code, it would be much appreciated.

  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: QSqlite issues

    First listing line 1 creates a connection named "timing". Second listing line 4 creates a query that is using the default database, not the named database connection "timing". In your program there must be a default database, and that is where the timing table is being created.

    Change line 4 to read:
    Qt Code:
    1. QSqlQuery timeQuery(timeDatabase);
    To copy to clipboard, switch view to plain text mode 

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

    duave (4th April 2011)

  4. #3
    Join Date
    Jun 2006
    Posts
    64
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite issues

    duave,

    The only thing that I see strange with your code is the following line but then I am no expert either.

    Qt Code:
    1. timingDatabase = QSqlDatabase::addDatabase("QSQLITE", "timing");
    To copy to clipboard, switch view to plain text mode 

    I am not sure about the "timing" being in the definition. To do what you want I did it this way:

    Qt Code:
    1. timeQuery.exec( "CREATE TABLE IF NOT EXISTS timing ("
    2. "`line1` varchar(10) default NULL,"
    3. "`line2` varchar(10) default NULL,"
    4. "`line3` varchar(10) default NULL)" );
    5. if (!timeQuery.isActive() )
    6. {
    7. QMessageBox::critical(this, "Error", "Table Creation Failed! \n"
    8. "\n"+dbqry.lastError().text()+"n"+timingDatabase.driverName() );
    9. }
    To copy to clipboard, switch view to plain text mode 

    I have an application that requires three tables and I use this method to check each table is there. If not, its created. Also, the the database is created if not there either.

    Hope this helps.

    B1.

Similar Threads

  1. QSqlite changes between 4.3 and 4.7
    By LKIM in forum Qt Programming
    Replies: 0
    Last Post: 22nd March 2011, 19:50
  2. QSQlite driver
    By praveen_g in forum Newbie
    Replies: 6
    Last Post: 18th November 2009, 08:58
  3. QSqlite in QT4
    By sophister in forum Qt Programming
    Replies: 26
    Last Post: 5th April 2009, 12:52
  4. QSQLITE Questions
    By larry104 in forum Qt Programming
    Replies: 9
    Last Post: 24th April 2007, 23:54
  5. QSqlite problem
    By dragon in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2007, 02:02

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.