LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   SQLite and C++ (https://www.linuxquestions.org/questions/programming-9/sqlite-and-c-544036/)

ManuPeng 04-07-2007 09:32 AM

SQLite and C++
 
Hello linux brothers,

I'm having a weird problem with SQLite on linux...
The code is fine, it compiles and all but I can't read back the dummy value I've inserted into the table.

I'm warning you, this is wild code because I always try to get things to work first, I clean it up when that's done:
Code:

QSqlDatabase *db = QSqlDatabase::addDatabase("QSQLITE");
db->setDatabaseName("TeST.db");
bool ok = db->open();
qDebug( "Opening database = %i", ok );

db->exec( "CREATE TABLE Test(mytext text);");
QString str = db->lastError().text();
qDebug( "LastQuery CREATE= %s", str.ascii() );

db->exec( "INSERT INTO Test(mytext) VALUES('Dummy');");
str = db->lastError().text();
qDebug( "LastQuery INSERT= %s", str.ascii() );

QSqlQuery answer = db->exec( "SELECT * FROM Test;");
str = db->lastError().text();
qDebug( "LastQuery SELECT= %s", str.ascii() );

str = answer.value(0).toString();
qDebug( "LastQuery VALUE= %s", str.ascii() );

db->close();

The debug messages I get (note that no answer as for CREATE, INSERT and SELECT means "all good", otherwise I get long errors when I mess up):
Opening database = 1
LastQuery CREATE=
LastQuery INSERT=
LastQuery SELECT=
QSqlQuery::value: not positioned on a valid record
LastQuery VALUE= (null)
*** Exited normally ***

I tried to have a look into the database using the command line tool sqlite3 and I can open the database fine, but when I try the following:
/debug/src> sqlite3 TeST.db
SQLite version 3.3.8
Enter ".help" for instructions
sqlite> SELECT * FROM Test;
SQL error: file is encrypted or is not a database
sqlite> .quit
/debug/src>

Now, I didn't encrypt the database, I swear to God!!

Manu

ManuPeng 04-07-2007 10:10 AM

Forget it
 
I hate to do this again, but I figured it out after all.

When I post here, I expect a prompt answer while I keep on searching on my end. And for the third time in 24h, I won the race.

Here's the changed part for the record:
Code:

qDebug( "LastQuery SELECT= %s", str.ascii() );

answer.first();
int i = answer.at();

str = answer.value(i).toString();
qDebug( "LastQuery VALUE= %s", str.ascii() );

Oh well, thanks anyways...

Kristofer 04-07-2007 10:15 AM

Incompatible versions
 
Hi ManuPeng

You probably have different versions of SQLite included in qt vs. the command line tools I'm guessing that Qt's database wrapper is linked against an older version of SQLite (2.8.2), and from what I see you are using the SQLite 3.3.8 command line tools.

/Kristofer

EDIT: For the second time in 30min I'm late posting my answer :(, although this time my answer seems to complete yours, or?

ManuPeng 04-07-2007 10:29 AM

Yeah, thanks!
 
You're right, your answer does complete mine, thanks for your help.

Your post made me realize that Qt3 still uses SQLite2 whereas the command line tool uses SQLite3, so that's certainly it.

I think there's a command line tool for SQLite2 on the DVD, I sort of remember seeing it.

From what I've read, Qt4 will implement SQLite3 which I like because it allows BLOBs and SQLite2 apparently doesn't. So I'll have to encode/decode in base64. I want to wait for a stable KDE4 release before I start messing up with my system. If it ain't broke...

Manu

ManuPeng 04-07-2007 10:38 AM

I confirm, it does it, the different versions of the SQLite used in Qt3 and in the command line tool was the problem. All good now!


All times are GMT -5. The time now is 09:05 AM.