Results 1 to 4 of 4

Thread: SQLITE insert problem

  1. #1
    Join Date
    Jan 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default SQLITE insert problem

    Hy everyone,

    I'm really struggling with a strange SQLITE database problem. I'm working under Linux (Slackware 13.37), Qt SDK 4.7.4.

    My application opens a database in the "main" function like this:

    Qt Code:
    1. db = QSqlDatabase::addDatabase ("QSQLITE");
    2. db.setDatabaseName (QCoreApplication::applicationDirPath () + "/mydb");
    3. if (!db.open ())
    4. qDebug () << "error opening database";
    5. else
    6. qDebug () << "database opened";
    To copy to clipboard, switch view to plain text mode 

    Up to here, all fine. Every 10 minutes it has to write a new line into a table. Before doing that, it makes a SELECT operation on the same table to check if there is already data identified by the same key (there are reasons for this, which are not important in our context).

    The table SQL schema is:
    Qt Code:
    1. CREATE TABLE consumi
    2. (
    3. year INTEGER NOT NULL,
    4. month INTEGER NOT NULL,
    5. day INTEGER NOT NULL,
    6. hour INTEGER NOT NULL,
    7. data1 INTEGER NOT NULL,
    8. data2 INTEGER NOT NULL,
    9. PRIMARY KEY (year, month, day, hour)
    10. );
    To copy to clipboard, switch view to plain text mode 

    And this is the code:

    Qt Code:
    1. bool stop = false;
    2.  
    3. QString sel = QString ("SELECT year FROM consumi WHERE year=%1 AND month=%2 AND day=%3 AND hour=%4")
    4. .arg (year)
    5. .arg (month)
    6. .arg (day)
    7. .arg (hour);
    8.  
    9.  
    10. if (!q.exec (sel))
    11. {
    12. qDebug () << "error during the SELECT query";
    13. qDebug () << q.lastError ();
    14. }
    15. else
    16. {
    17. if (q.next ())
    18. {
    19. qDebug () << "data already exists for this key";
    20. stop = true;
    21. }
    22. }
    23.  
    24. q.clear ();
    25.  
    26.  
    27. if (!stop)
    28. {
    29. QString s = QString ("INSERT INTO consumi VALUES (\"%1\", \"%2\", \"%3\", \"%4\", \"%5\", \"%6\")")
    30. .arg (year)
    31. .arg (month)
    32. .arg (day)
    33. .arg (hour)
    34. .arg (data1)
    35. .arg (data2);
    36.  
    37. if (!q1.exec (s))
    38. {
    39. qDebug () << "error during the INSERT query";
    40. qDebug () << q1.lastError ();
    41. }
    42.  
    43. if (q1.isActive ())
    44. qDebug () << "all fine";
    45. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately, this is what i always get:

    error during the INSERT query
    QSqlError(14, "Unable to fetch row", "unable to open database file")

    Actually, the database is open as every time the SELECT statement is executed without problems. I added the "q1.clear ()" call after reading about it somewhere, but it did not solve the problem. Things don't go better if I omit the SELECT query: no way at all.

    I think I tried all I could think about... I would appreciate some hints.

    Thank you!
    Roberto

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: SQLITE insert problem

    The code that you posted works fine here.
    Attached Files Attached Files

  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: SQLITE insert problem

    Tips: You use single quotes around strings in SQL, not double quotes (line 31). In your table definition and select query you treat year, month, day, and hour as numbers (line 3) and in your insert you treat them as strings. All your columns are integer types not strings. You should get into the habit of using QSQlQuery::prepare() and bindValue() rather than constructing queries as strings because it removes bunches of security issues and handles quoting properly for you.

  4. #4
    Join Date
    Jan 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SQLITE insert problem

    Dear norobro and ChrisW67, thank you for your answers. And: solved!!!

    @norobro: a difference between your test program and my software is that you create the database file, while I open an existing one. So I slightly modified yours (commenting out the CREATE TABLE query), put it on the same machine of the other and run in the same conditions. And I got it: it sounds stupid, but it was a permission problem. But... I was not helped by the SQLITE driver error messages.

    If the user launching the program does not have write permission on the existing database FILE, you get the error "attempt to write a readonly database" while attempting to INSERT. But if you have write permission on the database file, but not on the DIRECTORY which contains it, then you get "unable to open database file".

    So: I was not careful about it, but I think the error message could be more pertinent.

    @ChrisW67: I totally agree with you, but there's a reason why I do this. Time ago I had problems with another DB project because a lot of the numerous queries worked perfectly on the development machine, but not on the target machine, giving some strange errors (same libraries versions!!!). At the end I found out that the problem was with the QSqlQuery prepare/bindValue mechanism (I found some informations about this in a forum, maybe this one), which seemed to be buggy. So I had to rewrite all the queries using QString and arg, like you've seen here. Since then I just do directly like this.

    Actually, inserting as strings (thanks for the hint about the single/double quotes) was a desperate way to try to solve my problem... originally I used no quotes. Anyway, AFAIK, in SQLITE has a particular way to deal with data types (http://www.sqlite.org/datatype3.html), and even using quotes here is ok. But you're perfectly right about good programming techniques.

    Thanks!
    Roberto

Similar Threads

  1. Insert unicode in SQlite
    By Kastagne in forum Qt Programming
    Replies: 3
    Last Post: 11th October 2011, 14:07
  2. Baisc SQLite insert - does not work
    By johnnyturbo3 in forum Newbie
    Replies: 18
    Last Post: 12th August 2010, 14:15
  3. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 12:22
  4. Sqlite multipule row insert question
    By rogerholmes in forum Newbie
    Replies: 5
    Last Post: 31st December 2009, 16:36
  5. Bulk insert into SQLite
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2007, 03:56

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.