Results 1 to 7 of 7

Thread: QSQLITE driver issue

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

    Default QSQLITE driver issue

    G'Day,

    I am trying to write a simple app using Sqlite. (normally use MySql). My problem is that it creates the file "nksalvo.db", says that it is open, but the "create" commands fail saying the driver is not loaded. The "table 1 creation" message return the driver name as QSQLITE so it does appear to be loaded.

    I am using Qt 4.5.0 statically compiled, on windows ( at the moment - Linux next )

    I am guessing the error is somewhere else, but where?

    Any pointers would much appreciated...thanks B1.

    Qt Code:
    1. int MainWindow::openDatabase()
    2. {
    3. if (dbopen == true )
    4. {
    5. return 0;
    6. }
    7. else
    8. {
    9. // Create connection to database server
    10. nksalvoDB = QSqlDatabase::addDatabase( "QSQLITE" );
    11. nksalvoDB.setDatabaseName( cwdir+"/nksalvo.db" );
    12. if ( !nksalvoDB.open() )
    13. {
    14. QMessageBox::critical( this, "NKSalvo Database Error",
    15. "Could not connect to the database!\n"
    16. "\n"
    17. "Reported error:\n"
    18. +nksalvoDB.lastError().driverText()+"\n"
    19. +nksalvoDB.lastError().databaseText()+"\n"
    20. +nksalvoDB.databaseName()+"\n");
    21. dbopen = false;
    22. return 1;
    23. }
    24. createtableqry.prepare( "CREATE TABLE IF NOT EXISTS serialdata (`port` varchar(10) default NULL,"
    25. "`speed` varchar(6) default NULL,`databits` varchar(4) default NULL,"
    26. "`parity` varchar(5) default NULL, `stopbits` varchar(4) default NULL"
    27. "`id` smallint(2) NOT NULL, PRIMARY KEY (`id`)" );
    28. createtableqry.exec();
    29. if (!createtableqry.isActive() )
    30. {
    31. QMessageBox::critical(this, "Salvo Error", "Table 1 Creation Failed! \n"
    32. "\n"+createtableqry.lastError().text()+"\n"+nksalvoDB.driverName() );
    33. dbopen = false;
    34. return 1;
    35. }
    36. createtableqry.prepare( "CREATE TABLE IF NOT EXIST salvodata (butlabel string, butcolour string, row1 string,"
    37. "row2 string, row3 string, row4 string, row5 string,"
    38. "row6 string, row7 string, row8 string, row9 string,"
    39. "row10 string, id int NOT NULL, PRIMARY KEY (`id`))");
    40. createtableqry.exec();
    41. if (!createtableqry.isActive() )
    42. {
    43. QMessageBox::critical(this, "Salvo Error", "Table 2 Creation Failed! \n"
    44. "\n"+createtableqry.lastError().text()+"\n"+nksalvoDB.driverName());
    45. dbopen = false;
    46. return 1;
    47. }
    48. dbopen = true;
    49. return 0;
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by b1; 17th May 2009 at 06:30. Reason: Added more info....

  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: QSQLITE driver issue

    • What does
      Qt Code:
      1. qWarning() << QSqlDatabase::drivers();
      To copy to clipboard, switch view to plain text mode 
      tell you?
    • Use exec("CREATE...") there is no need to use prepare.


    From the docs:
    Returns true if the query is active. An active QSqlQuery is one that has been exec()'d successfully but not yet finished with.
    So maybe the create statement was successful but already finished, because a normal CREATE don't need much time. Have you checked your created database file, I guess the table is created there...


    EDIT: and, ähh, your sql statements aren't valid!!!

  3. #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 driver issue

    Thanks for the quick response Lykurg,

    It gives ("QMYSQL3", "QMYSQL", "QSQLITE")

    And thanks for the tip on exec( Create....)

    The SQL statements are taken from similar ones I use under MySQL. Granted the second statement is probably wrong, but I can't see whats wrong with the first statement.

    B1.
    Last edited by b1; 17th May 2009 at 08:00. Reason: More info....

  4. #4
    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: QSQLITE driver issue

    Quote Originally Posted by b1 View Post
    Granted the second statement is probably wrong, but I can't see whats wrong with the first statement.
    CREATE TABLE IF NOT EXISTS serialdata (`port` varchar(10) default NULL, `speed` varchar(6) default NULL,`databits` varchar(4) default NULL, `parity` varchar(5) default NULL, `stopbits` varchar(4) default NULL `id` smallint(2) NOT NULL, PRIMARY KEY (`id`)
    should be
    CREATE TABLE IF NOT EXISTS serialdata (`port` varchar(10) default NULL, `speed` varchar(6) default NULL,`databits` varchar(4) default NULL, `parity` varchar(5) default NULL, `stopbits` varchar(4) default NULL, `id` smallint(2) NOT NULL, PRIMARY KEY (`id`))
    Note the last ")" and the comma in front of "`id` smallint(2)".

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

    Default Re: QSQLITE driver issue

    D'OH!! Thank you Lykurg,

    I think I need glasses!! Thank you for pointing out my typos. I have now corrected them but I still have the same error.
    I added the qWarning << nksalvoDB.open() as below
    Qt Code:
    1. createtableqry.exec( "CREATE TABLE IF NOT EXISTS serialdata (`port` varchar(10) default NULL,"
    2. "`speed` varchar(6) default NULL,`databits` varchar(4) default NULL,"
    3. "`parity` varchar(5) default NULL, `stopbits` varchar(4) default NULL,"
    4. "`id` smallint(2) NOT NULL, PRIMARY KEY (`id`) )" );
    5.  
    6. if (!createtableqry.isActive() )
    7. {
    8. QMessageBox::critical(this, "Salvo Error", "Table 1 Creation Failed! \n"
    9. "\n"+createtableqry.lastError().text()+"\n"+nksalvoDB.driverName() );
    10.  
    11. dbopen = false;
    12. qWarning() << QSqlDatabase::drivers();
    13. qWarning() << nksalvoDB.open();
    14. return 1
    To copy to clipboard, switch view to plain text mode 
    and it returns true!

    So I am guessing its an SQL syntax problem then...but I can't see it!

    Thank you again...B1.

  6. #6
    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: QSQLITE driver issue

    Ok, your code works fine on my side. So next guess createtableqry points the the wrong database connection. Where do you initialize it? Use for testing a
    Qt Code:
    1. createtableqry = QSqlQuery(nksalvoDB);
    To copy to clipboard, switch view to plain text mode 
    right bevor you use it.

    And it's no good idea to have a QSqlQuery as a member variable, can easily cause errors on exit the application like "Closing xxx while database xx still in use" or something like that.

  7. The following user says thank you to Lykurg for this useful post:

    b1 (17th May 2009)

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

    Default Re: QSQLITE driver issue

    Hooray!!!

    Thank you so much, Lykurg. That did the trick. I will re visit how I initialise the database query.

    Thanks again, I do appreciate it.
    A very happy B1.

Similar Threads

  1. QDevelop SQLite Driver Issue
    By Phan Sin Tian in forum Qt-based Software
    Replies: 4
    Last Post: 1st August 2010, 11:47
  2. Possible OpenGL / Intel driver issue
    By Truder in forum Qt Programming
    Replies: 0
    Last Post: 24th February 2009, 11:06
  3. SQLite driver unavailable
    By kandalf in forum Installation and Deployment
    Replies: 5
    Last Post: 11th February 2009, 16:36
  4. How do I get Qt to recognize the oci driver.
    By yleesun in forum Qt Programming
    Replies: 11
    Last Post: 19th January 2009, 03:50
  5. Replies: 1
    Last Post: 7th July 2008, 20:13

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.