rdf
22nd September 2011, 09:25
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:
db = QSqlDatabase::addDatabase ("QSQLITE");
db.setDatabaseName (QCoreApplication::applicationDirPath () + "/mydb");
if (!db.open ())
qDebug () << "error opening database";
else
qDebug () << "database opened";
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:
CREATE TABLE consumi
(
year INTEGER NOT NULL,
month INTEGER NOT NULL,
day INTEGER NOT NULL,
hour INTEGER NOT NULL,
data1 INTEGER NOT NULL,
data2 INTEGER NOT NULL,
PRIMARY KEY (year, month, day, hour)
);
And this is the code:
bool stop = false;
QString sel = QString ("SELECT year FROM consumi WHERE year=%1 AND month=%2 AND day=%3 AND hour=%4")
.arg (year)
.arg (month)
.arg (day)
.arg (hour);
QSqlQuery q;
if (!q.exec (sel))
{
qDebug () << "error during the SELECT query";
qDebug () << q.lastError ();
}
else
{
if (q.next ())
{
qDebug () << "data already exists for this key";
stop = true;
}
}
q.clear ();
QSqlQuery q1;
if (!stop)
{
QString s = QString ("INSERT INTO consumi VALUES (\"%1\", \"%2\", \"%3\", \"%4\", \"%5\", \"%6\")")
.arg (year)
.arg (month)
.arg (day)
.arg (hour)
.arg (data1)
.arg (data2);
if (!q1.exec (s))
{
qDebug () << "error during the INSERT query";
qDebug () << q1.lastError ();
}
if (q1.isActive ())
qDebug () << "all fine";
}
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
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:
db = QSqlDatabase::addDatabase ("QSQLITE");
db.setDatabaseName (QCoreApplication::applicationDirPath () + "/mydb");
if (!db.open ())
qDebug () << "error opening database";
else
qDebug () << "database opened";
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:
CREATE TABLE consumi
(
year INTEGER NOT NULL,
month INTEGER NOT NULL,
day INTEGER NOT NULL,
hour INTEGER NOT NULL,
data1 INTEGER NOT NULL,
data2 INTEGER NOT NULL,
PRIMARY KEY (year, month, day, hour)
);
And this is the code:
bool stop = false;
QString sel = QString ("SELECT year FROM consumi WHERE year=%1 AND month=%2 AND day=%3 AND hour=%4")
.arg (year)
.arg (month)
.arg (day)
.arg (hour);
QSqlQuery q;
if (!q.exec (sel))
{
qDebug () << "error during the SELECT query";
qDebug () << q.lastError ();
}
else
{
if (q.next ())
{
qDebug () << "data already exists for this key";
stop = true;
}
}
q.clear ();
QSqlQuery q1;
if (!stop)
{
QString s = QString ("INSERT INTO consumi VALUES (\"%1\", \"%2\", \"%3\", \"%4\", \"%5\", \"%6\")")
.arg (year)
.arg (month)
.arg (day)
.arg (hour)
.arg (data1)
.arg (data2);
if (!q1.exec (s))
{
qDebug () << "error during the INSERT query";
qDebug () << q1.lastError ();
}
if (q1.isActive ())
qDebug () << "all fine";
}
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