Results 1 to 19 of 19

Thread: Baisc SQLite insert - does not work

  1. #1
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Baisc SQLite insert - does not work

    Hi,

    The code below should insert new data into the table. But it doesn't. Can anybody see anything wrong?

    Thanks

    Qt Code:
    1. #include <QApplication>
    2. #include <QTSql>
    3. #include <QMessageBox>
    4. #include <QDebug>
    5. #include <QString>
    6. #include <fstream>
    7. #include <String>
    8. #include <QSqlQuery>
    9. using namespace std;
    10.  
    11.  
    12. bool createConnection(){
    13. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    14. db.setDatabaseName("ManagerDB.db");
    15. if(!db.open()){
    16. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    17. qApp->tr("Unable to establish a database connection.\n"
    18. "This program needs SQLite support. Please read "
    19. "the Qt SQL driver documentation for information how "
    20. "to build it.\n\n"
    21. "Click Cancel to exit."), QMessageBox::Cancel);
    22. return false;
    23. }
    24.  
    25. return true;
    26. }
    27.  
    28.  
    29.  
    30. void insertData(){
    31.  
    32. QSqlQuery query("INSERT INTO table1 (field1) "
    33. "VALUES (""\"203""\")");
    34. query.exec();
    35. }
    36.  
    37.  
    38.  
    39. int main(int argc, char *argv[]){
    40. QApplication app(argc, argv);
    41. if (!createConnection()){
    42. return false;
    43. }
    44. insertData();
    45. return app.exec();
    46. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Baisc SQLite insert - does not work

    Does table "table1" exist?

  3. #3
    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: Baisc SQLite insert - does not work

    your " are really a mess. See QSqlQuery::prepare().

  4. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Baisc SQLite insert - does not work

    Your query is not hooked to a database connection. You have to do this before the query.prepare statement.

    If you look at this: http://doc.trolltech.com/4.5/qtsql.html#details

    You can see how to do it.
    as Lykurg said,

    query.prepare(...
    query.addBindValue(....
    query.exec();

  5. #5
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Baisc SQLite insert - does not work

    Thanks for the replies.

    The database and 'table1' do exist.

    The code now looks like this has been changed to use 'prepare', but unfortunately, it still doesn't work. Looking at examples for the web and official docs I can't see what is wrong with what is written. I am using NetBeans, so I have checked the SQL check box in the project properties in order to use the SQL libraries.

    I use SQLite Admin http://sqliteadmin.orbmu2k.de/ to check to see if the data has been inserted. I've tried running the program with SQLite Admin both running and off - makes no difference.

    Qt Code:
    1. #include <QApplication>
    2. #include <QTSql>
    3. #include <QMessageBox>
    4. #include <QString>
    5. #include <String>
    6. #include <QSqlQuery>
    7. using namespace std;
    8.  
    9.  
    10.  
    11. bool createConnection(){
    12. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    13. db.setDatabaseName("C:\\NetBeans Projects\\SQLiteExample\\ManagerDB.db");
    14. if(!db.open()){
    15. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    16. qApp->tr("Something bad happened"), QMessageBox::Cancel);
    17. return false;
    18. }
    19.  
    20. QSqlQuery query;
    21. query.prepare("INSERT INTO table1 (field1) VALUES (:field1)");
    22. query.bindValue(":field1", "work damn it");
    23. query.exec();
    24. db.close();
    25. return true;
    26. }
    27.  
    28.  
    29.  
    30. int main(int argc, char *argv[]){
    31. QApplication app(argc, argv);
    32. if(!createConnection()){
    33. return false;
    34. }
    35. return app.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Baisc SQLite insert - does not work

    Do I need to build a SQLite driver like MySQL? I've read that SQLite is comes included in the Qt installation.

  7. #7
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Baisc SQLite insert - does not work

    You are getting close, and frustrated as I can understand.
    Unfortunately, the doc is a little fragmented and doesn't really give you a good complete example. We have all been through that before.

    The problem is, as I stated earlier, that your query is not 'hooked' to your database connection.
    Here is an example that might help:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "myConnection");
    2. db.setDatabaseName("myDatabase.sqlite");
    3. db.open();
    4. QSqlQuery query(db);
    5. query.prepare("SELECT max(id) from myTable where call = ? ");
    6. query.addBindValue(searchCall);
    7. query.exec();
    8. query.last();
    9. // now you can use debug to see the result
    To copy to clipboard, switch view to plain text mode 

    So what you need to change in your code is:
    1. the database file name extension to .sqlite
    2. QSqlQuery query; to QSqlQuery query(db); // This is the 'hook'

    Good luck and post again if you get stuck.

  8. #8
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Baisc SQLite insert - does not work

    Thanks for your help. Unfortunately, it still doesn't add data to my database. I created a new database with the .sqlite file extension, and with a table called table1 that has a field called field1.

    I managed to get the same program to work with MySQL, so, I have no idea what is wrong.

    Qt Code:
    1. bool createConnectionSQLite(){
    2. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","myConnection");
    3. db.setDatabaseName("C:\\NetBeans Projects\\SQLiteExample\\myDataBase.sqlite");
    4. db.open();
    5. if (!db.open()) {
    6. QMessageBox::critical(0, QObject::tr("Database Error"),
    7. db.lastError().text());
    8. return false;
    9. }
    10. QSqlQuery query(db);
    11. query.prepare("INSERT INTO table1 (field1) VALUES (:field1)");
    12. query.bindValue(":field1", "work damn it");
    13. query.exec();
    14. db.close();
    15. return true;
    16. }
    17.  
    18.  
    19. int main(int argc, char *argv[]){
    20. QApplication app(argc, argv);
    21. if(!createConnectionSQLite()){
    22. return false;
    23. }
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Baisc SQLite insert - does not work

    How do you create the database and tables? If you are using other application, try to create your database and tables by code.

  10. #10
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Baisc SQLite insert - does not work

    I seems to me that trouble might be in this line
    Qt Code:
    1. db.setDatabaseName("C:\\NetBeans Projects\\SQLiteExample\\myDataBase.sqlite");
    To copy to clipboard, switch view to plain text mode 
    Use "/" instead of "\\". I believe that in Qt file path separator is always "/" regardless of OS on which application is developed or compiled. See QFile Detailed Description section.

    Connect to database can be done without giving it name e.g. "myConnection", and in that case connection becomes default connection for particular application, and in that case it is not necessary to pass db as argument to a query constructor.

    I do not get what is result of query.exec() when you debug? Check QSqlQuery::lastError() and QSqlQuery::numRowsAffected () functions;

    Wild guess try opening transaction and committing it.

    If you run this app, if it fails it will return 0 which means that it exited ok. If something is wrong app should exit with int value different then 0. In main.cpp file there is line return false; (int)false = 0 !
    Last edited by pervlad; 11th August 2010 at 15:08.

  11. #11
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Baisc SQLite insert - does not work

    I've tried all sorts of combinations. Defining absolute file path and omitting it. My first version of the code did not use a connection name, and had no query constructor.

    Is there a way to test whether the SQLite plug-ins are installed/where they should be? If the syntax is okay, then there must be something wrong with my installation.

  12. #12
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Baisc SQLite insert - does not work

    Ok, if you tried replacing "\\" with "/".
    There is a way to check is driver loaded: QSqlDatabase::isDriverAvailable(), QSqlDatabase::drivers().
    I the beck of my mind I remember that somewhere in Qt documentation there are articles on how to install other sql drivers.
    Did you try to build Qt sql lite example Cached Table which works with db stored in memory?

  13. #13
    Join Date
    Jul 2010
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Baisc SQLite insert - does not work

    I have built Qt example "Cached Table" which works with SqlLite in-memory database and it works i.e. connects to database. I changed one line in static bool createConnection() function
    db.setDatabaseName(":memory:");
    in to:
    db.setDatabaseName("F:/Temp/test.sdb");//(":memory:");
    Application created file "F:/Temp/test.sdb" and changes I have committed become permanent. I have not intall nor Qt SqlLite drivers nor SqlLite db engine it self, thus SqlLite db drvers are distributed and installed together with Qt distribution.

  14. #14
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Baisc SQLite insert - does not work

    Quote Originally Posted by johnnyturbo3 View Post
    it still doesn't add data to my database
    What is the return value from QSqlQuery::exec and in case it is false, what is the output from QSqlQuery::lastError? You can use qDebug to print the values to output window.

  15. #15
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Baisc SQLite insert - does not work

    query.exec() is returning 'false'
    error: 'Parameter count mismatch'

  16. #16
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Baisc SQLite insert - does not work

    Can you please copy/paste the piece of code that you used to create the SQL table?

  17. #17
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Baisc SQLite insert - does not work

    Some success now.
    Previously, I created the database and table using other means i.e. not code. However, I included a CREATE TABLE statement in the code and it now seems to work.
    Ultimately, I would like the program to read from an existing database and table created by another program.


    Qt Code:
    1. QSqlQuery query(db);
    2. query.prepare("CREATE TABLE table1 (field1 varchar(50))");
    3. query.exec();
    4. query.prepare("INSERT INTO table1 (field1) VALUES (:field1)");
    5. query.bindValue(":field1", "work damn it");
    6. if(query.exec() == true){
    7. QMessageBox::critical(0, QObject::tr("Database Success"),
    8. db.lastError().text());
    9. }else{
    10. QMessageBox::critical(0, QObject::tr("Database Error"),
    11. query.lastError().text());
    12. }
    13. db.close();
    To copy to clipboard, switch view to plain text mode 

    Thanks

  18. #18
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Baisc SQLite insert - does not work

    Quoting from Qt Doc.

    QSQLITE File Format Compatibility

    SQLite minor releases sometimes break file format forward compatibility. For example, SQLite 3.3 can read database files created with SQLite 3.2, but databases created with SQLite 3.3 cannot be read by SQLite 3.2. Please refer to the SQLite documentation and change logs for information about file format compatibility between versions.

    Qt minor releases usually follow the SQLite minor releases, while Qt patch releases follow SQLite patch releases. Patch releases are therefore both backward and forward compatible.
    Maybe, this explains why you fail inserting record into your table. I guess, SQLite Administrator using newer version of SQLite than SQLite provided by Nokia.

    I rebuild qsqlite plugin with the newest sqlite sources(version 3.7.0.1), then create database using SQLite Administrator. I successfully insert/update/delete record and etc.

  19. #19
    Join Date
    Aug 2010
    Posts
    30
    Thanks
    2

    Default Re: Baisc SQLite insert - does not work

    That must be it.
    I've switched from SQLite Admin to the FireFox plugin SQLite Manager to create my test DB's and everything seems to be working.......for now.

    Thanks everyone for your help!

Similar Threads

  1. Insert unicode in SQlite
    By Kastagne in forum Qt Programming
    Replies: 3
    Last Post: 11th October 2011, 14:07
  2. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 12:22
  3. Sqlite multipule row insert question
    By rogerholmes in forum Newbie
    Replies: 5
    Last Post: 31st December 2009, 16:36
  4. SQLite sometimes doens't INSERT into database
    By cevou in forum Qt Programming
    Replies: 5
    Last Post: 30th October 2009, 08:10
  5. Bulk insert into SQLite
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2007, 03:56

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.