[QT4][SQLITE] Database and query
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. :
Code:
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:
.cpp file:
Code:
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...
Re: [QT4][SQLITE] Database and query
Re: [QT4][SQLITE] Database and query
This doesn't really help because I already read that (and more).
Code:
db.setDatabaseName(databaseFile->filePath());
Code:
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...
Re: [QT4][SQLITE] Database and query
Is this code not giving you any error during compilation?
Try this
Code:
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.");
}
Re: [QT4][SQLITE] Database and query
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 ?
Re: [QT4][SQLITE] Database and query
Quote:
Originally Posted by agent007se
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.
Quote:
Originally Posted by agent007se
Do I have to install some specific programs ?
No you don't have to.
what is databaseFile->filePath()?
Re: [QT4][SQLITE] Database and query
Quote:
Originally Posted by munna
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:
in CDatabase.cpp:
Code:
// constructor
CDatabase
::CDatabase(QString dbName
){
CDatabase::dbName = databaseFile->fileName();
}
in SomeFile.cpp:
Code:
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");
Quote:
Originally Posted by munna
Just see in the public functions in QSqlQuery :
Quote:
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...
Re: [QT4][SQLITE] Database and query
try to append a semicolon (" ; ") to the sqlstring:
Code:
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..
Re: [QT4][SQLITE] Database and query
with AND without a semicolon :
tmp value:
Quote:
"near "test": syntax error Unable to execute statement"
text() function
:confused:
Re: [QT4][SQLITE] Database and query
Quote:
Originally Posted by agent007se
"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
Re: [QT4][SQLITE] Database and query
After you call this:
- QSqlDatabase db = QSqlDatabase::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.