Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-07-2007, 09:32 AM   #1
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Rep: Reputation: 15
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:
QSqlDatabase *db = QSqlDatabase::addDatabase("QSQLITE");
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() );

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

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


Last edited by ManuPeng; 04-07-2007 at 09:34 AM.
Old 04-07-2007, 10:10 AM   #2
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Original Poster
Rep: Reputation: 15
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:
qDebug( "LastQuery SELECT= %s", str.ascii() );

int i =;

str = answer.value(i).toString();
qDebug( "LastQuery VALUE= %s", str.ascii() );
Oh well, thanks anyways...
Old 04-07-2007, 10:15 AM   #3
LQ Newbie
Registered: Nov 2005
Location: Sweden
Distribution: Slackware
Posts: 20

Rep: Reputation: 0
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.


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

Last edited by Kristofer; 04-07-2007 at 10:18 AM.
Old 04-07-2007, 10:29 AM   #4
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Original Poster
Rep: Reputation: 15
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...


Last edited by ManuPeng; 04-07-2007 at 10:35 AM.
Old 04-07-2007, 10:38 AM   #5
Registered: Mar 2006
Location: Germany
Distribution: OpenSuse 10.2
Posts: 54

Original Poster
Rep: Reputation: 15
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!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Replication and SQLite krizzz Programming 3 12-01-2015 03:45 PM
SQLite JavaWrapper netjack Linux - Software 0 05-10-2006 01:22 AM
pysqlite and sqlite hawking Red Hat 1 01-09-2006 01:14 PM
sqlite output Liakoni Linux - Software 1 10-07-2005 05:57 AM
sqlite and bash verstapp Linux - General 0 08-07-2004 05:49 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration