LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 04-07-2007, 10:32 AM   #1
ManuPeng
Member
 
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:
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

Last edited by ManuPeng; 04-07-2007 at 10:34 AM.
 
Old 04-07-2007, 11:10 AM   #2
ManuPeng
Member
 
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:
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...
 
Old 04-07-2007, 11:15 AM   #3
Kristofer
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.

/Kristofer

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 11:18 AM.
 
Old 04-07-2007, 11:29 AM   #4
ManuPeng
Member
 
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...

Manu

Last edited by ManuPeng; 04-07-2007 at 11:35 AM.
 
Old 04-07-2007, 11:38 AM   #5
ManuPeng
Member
 
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!
 
  


Reply


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 1 06-16-2006 06:45 PM
SQLite JavaWrapper netjack Linux - Software 0 05-10-2006 02:22 AM
pysqlite and sqlite hawking Red Hat 1 01-09-2006 02:14 PM
sqlite output Liakoni Linux - Software 1 10-07-2005 06:57 AM
sqlite and bash verstapp Linux - General 0 08-07-2004 06:49 AM


All times are GMT -5. The time now is 04:57 AM.

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