PDA

View Full Version : [QT4][SQLITE] Database and query



agent007se
2nd July 2006, 07:40
Hello !

I'm trying to have a very little db. My knowledges in db are poor but I just want to know how can I set a minimal db i.e. :


MAIN_TREE
entry1
...(1->L)
entryL
SUB_TREE1
entry1
...(1->I)
entryI
SUB_TREE...(1->N)
entry1
...(1->J)
entryJ
SUB_TREEN
entry1
...(1->K)
entryK


The entries will just be a list of files (and their path).

I'm really noob because my very little code doesn't work :

.h file:


QSqlQuery *query;

.cpp file:


[post edit :] query = new QSqlQuery(db);
bool tst = query->exec("CREATE TABLE person");


=> lol. I have a segfault... edit : The segfault is gone but... there isn't any table in the file which is correctly created. The result of the query is "false". But I wonder why... it seems to be a basic query...

dexjam
2nd July 2006, 12:30
May be you have a look at

http://doc.trolltech.com/4.1/examples.html#sql-examples
and
http://doc.trolltech.com/qq/qq15-models.html

there is some basic code, which shows you how to use sqlite

agent007se
9th July 2006, 03:31
This doesn't really help because I already read that (and more).



QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(databaseFile->filePath());




QSqlQuery query = new QSqlQuery(db);
QString createQuery = "CREATE TABLE test";
bool tst = query->exec(createQuery);
if(tst)
{
// some code
}
else
{
QMessageBox::critical(NULL, "", "Query \"" + createQuery + "\" not executed."); // IT ALWAYS COMES
}


I'm trying hard but I'm doing something wrong...

munna
9th July 2006, 06:24
Is this code not giving you any error during compilation?

Try this




QSqlQuery query;
bool tst = query.exec("CREATE TABLE test(id INTEGER PRIMARY KEY,"
"name TEXT");
if(tst) {
// some code
} else {
QMessageBox::critical(NULL, "", "Query \"" + createQuery + "\" not executed.");
}

agent007se
9th July 2006, 06:44
I copy and paste your code and "tst" is still false (and the QMessageBox pops up).

There isn't any error or warning during compilation :D !

I begin to think that there is a problem with QSqlite but I think that it's an integrated db with Qt, isn't it ?

Do I have to install some specific programs ?

munna
9th July 2006, 06:49
I begin to think that there is a problem with QSqlite but I think that it's an integrated db with Qt, isn't it ?

Yes it is.


Do I have to install some specific programs ?

No you don't have to.

what is databaseFile->filePath()?



QSqlQuery query = new QSqlQuery(db);//This line should give error

agent007se
9th July 2006, 08:42
what is databaseFile->filePath()?


Isn't enough to know that the connect() function return a true value ?

Anyway here's the code you asked:

in CDatabase.h:


private:
QFileInfo *databaseFile;


in CDatabase.cpp:


// constructor
CDatabase::CDatabase(QString dbName)
{
databaseFile = new QFileInfo(dbName);
CDatabase::dbName = databaseFile->fileName();
}


in SomeFile.cpp:


static CDatabase *CDB;
...
// instantiation of CDB where stest is the return of a QFileDialog::getExistingDirectory (and is not a null string)
CDB = new CDatabase(stest + "/" + dbNameStr + ".db");






QSqlQuery query = new QSqlQuery(db);//This line should give error



Just see in the public functions in QSqlQuery :

QSqlQuery ( QSqlDatabase db )

Like I said, the compilation is nice ;-). I don't understand why there might be an error...

I use : Qt 4.1, Windows (mingw) and Code::Blocks...

aMan
9th July 2006, 08:47
try to append a semicolon (" ; ") to the sqlstring:

QString createQuery = "CREATE TABLE test;";

and if that doesn't help, try to get the error:
http://doc.trolltech.com/4.1/qsqlquery.html#lastError


regards aman..

agent007se
9th July 2006, 10:48
with AND without a semicolon :



QSqlError QSE = query.lastError();
QString tmp = QSE.text();
QMessageBox::warning(NULL, "", tmp);


tmp value:

"near "test": syntax error Unable to execute statement"

text() function (http://doc.trolltech.com/4.1/qsqlerror.html#text)

:confused:

jacek
9th July 2006, 15:14
"near "test": syntax error Unable to execute statement"
It looks like your query is wrong. Can you execute that statement from SQLite console?

I don't use SQLite, but usually "CREATE TABLE xxx" is not enough --- you must add column definitions.

http://www.sqlite.org/lang_createtable.html

gfunk
12th July 2006, 22:16
After you call this:

QSqlDatabase (http://doc.trolltech.com/4.1/QSqlDatabase.html) db = QSqlDatabase (http://doc.trolltech.com/4.1/QSqlDatabase.html)::addDatabase("QSQLITE");
db.setDatabaseName(databaseFile->filePath());do you call this?

bool ok = db.open();If it succeeds, ok should be true.
If it doesn't, then all the following exec() calls will probably fail too.