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