Results 1 to 11 of 11

Thread: [QT4][SQLITE] Database and query

  1. #1
    Join Date
    Jun 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [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. :
    Qt Code:
    1. MAIN_TREE
    2. entry1
    3. ...(1->L)
    4. entryL
    5. SUB_TREE1
    6. entry1
    7. ...(1->I)
    8. entryI
    9. SUB_TREE...(1->N)
    10. entry1
    11. ...(1->J)
    12. entryJ
    13. SUB_TREEN
    14. entry1
    15. ...(1->K)
    16. entryK
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. QSqlQuery *query;
    To copy to clipboard, switch view to plain text mode 
    .cpp file:
    Qt Code:
    1. [post edit :] query = new QSqlQuery(db);
    2. bool tst = query->exec("CREATE TABLE person");
    To copy to clipboard, switch view to plain text mode 

    => 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...
    Last edited by agent007se; 2nd July 2006 at 08:53.

  2. #2
    Join Date
    Jun 2006
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: [QT4][SQLITE] Database and query

    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

  3. #3
    Join Date
    Jun 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT4][SQLITE] Database and query

    This doesn't really help because I already read that (and more).

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(databaseFile->filePath());
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QSqlQuery query = new QSqlQuery(db);
    2. QString createQuery = "CREATE TABLE test";
    3. bool tst = query->exec(createQuery);
    4. if(tst)
    5. {
    6. // some code
    7. }
    8. else
    9. {
    10. QMessageBox::critical(NULL, "", "Query \"" + createQuery + "\" not executed."); // IT ALWAYS COMES
    11. }
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT4][SQLITE] Database and query

    Is this code not giving you any error during compilation?

    Try this

    Qt Code:
    1. QSqlQuery query;
    2. bool tst = query.exec("CREATE TABLE test(id INTEGER PRIMARY KEY,"
    3. "name TEXT");
    4. if(tst) {
    5. // some code
    6. } else {
    7. QMessageBox::critical(NULL, "", "Query \"" + createQuery + "\" not executed.");
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Arrow 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 !

    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 ?

  6. #6
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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()?

    Qt Code:
    1. QSqlQuery query = new QSqlQuery(db);//This line should give error
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jun 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Arrow 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:
    Qt Code:
    1. private:
    2. QFileInfo *databaseFile;
    To copy to clipboard, switch view to plain text mode 

    in CDatabase.cpp:
    Qt Code:
    1. // constructor
    2. CDatabase::CDatabase(QString dbName)
    3. {
    4. databaseFile = new QFileInfo(dbName);
    5. CDatabase::dbName = databaseFile->fileName();
    6. }
    To copy to clipboard, switch view to plain text mode 

    in SomeFile.cpp:
    Qt Code:
    1. static CDatabase *CDB;
    2. ...
    3. // instantiation of CDB where stest is the return of a QFileDialog::getExistingDirectory (and is not a null string)
    4. CDB = new CDatabase(stest + "/" + dbNameStr + ".db");
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by munna
    Qt Code:
    1. QSqlQuery query = new QSqlQuery(db);//This line should give error
    To copy to clipboard, switch view to plain text mode 
    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...

  8. #8
    Join Date
    Jul 2006
    Posts
    79
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT4][SQLITE] Database and query

    try to append a semicolon (" ; ") to the sqlstring:
    Qt Code:
    1. QString createQuery = "CREATE TABLE test;";
    To copy to clipboard, switch view to plain text mode 

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


    regards aman..

  9. #9
    Join Date
    Jun 2006
    Posts
    27
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Arrow Re: [QT4][SQLITE] Database and query

    with AND without a semicolon :

    Qt Code:
    1. QSqlError QSE = query.lastError();
    2. QString tmp = QSE.text();
    3. QMessageBox::warning(NULL, "", tmp);
    To copy to clipboard, switch view to plain text mode 

    tmp value:
    "near "test": syntax error Unable to execute statement"
    text() function


  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  11. #11
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: [QT4][SQLITE] Database and query

    After you call this:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(databaseFile->filePath());
    do you call this?
    1. 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.
    Software Engineer



Similar Threads

  1. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 18:50
  2. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 18:53
  3. Replies: 8
    Last Post: 7th March 2006, 14:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.