Results 1 to 10 of 10

Thread: Unable to work with database

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Unable to work with database

    Hi Guys,

    Not sure where I'm going wrong but here's my code:

    [CODE/] //Create a database (TO DO, close the database)
    QString strServerName = "LOCALHOSE\\SQLITE";
    QString strDBName = "test.db";

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //EDIT in final to incorporate multiple connections, will need a tracking variable that will track how many connections are opened and create a new unique one.
    /*
    db.setConnectOptions();
    QString dsn = QString("DRIVER={SQL Native Client}; SERVER=%1;DATABASE=%2;Trusted_Connection=Yes;").ar g(strServerName).arg(strDBName);
    db.setDatabaseName(dsn);
    */
    db.setDatabaseName(strDBName);

    bool dbOpen = db.open("test.db", NULL);
    if(dbOpen == TRUE)
    {
    qDebug() << "Database open";
    QSqlQuery query;
    query.exec("SELECT Row FROM tblField WHERE Page = one");
    qDebug() << query.lastError().text(); //if error executing SQL statement occurs, get error


    }
    if(dbOpen == FALSE)
    {
    qDebug() << "Last error " << db.lastError().text();
    }

    //Button cNav;

    db.close();
    db.removeDatabase("test.db");[\CODE]

    Basically I get the output of:

    Database open
    "no such table: tblField Unable to execute statement"

    Not sure why, I've got the database in the folder where the .exe is, in fact I've got in about 10 different folders just to be sure. I know that if it can't find a database, it'll create a new blank one, which I think is what is happening, question is, why?
    Last edited by Atomic_Sheep; 5th August 2013 at 12:26.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Unable to work with database

    Have you verified with an external tool that the database actually has a table called tblField?

    Also make sure your preprocessor macros are defined correctly or better even use C++ boolean literals true and false.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Unable to work with database

    This code will be looking for a file called "test.db" in the current working directory of the executing process. This is not necessarily in the same place as the executable or "in about 10 different folders just to be sure." For test purposes put a correct test.db file in a known location and specify an absolute path to it.

    Even if tblfield did exist this:
    Qt Code:
    1. query.exec("SELECT Row FROM tblField WHERE Page = one");
    To copy to clipboard, switch view to plain text mode 
    is probably not valid SQL unless there is a column actually called one.

    Qt Code:
    1. db.removeDatabase("test.db");
    To copy to clipboard, switch view to plain text mode 
    makes no sense, you never created a database connection called "test.db" with addDatabase().

  4. #4
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Unable to work with database

    I've been looking into how to add an existing database into a project, however I've not been able to figure out exactly how to do it and it's not really clearly documented in the tutorial. Here is what I presently have in the .pro file:

    Qt Code:
    1. LIBS += C:\Users\Administrator\Desktop\sqlite3.dll
    2.  
    3. addFiles.sources = test.db
    4. addFiles.path = .
    To copy to clipboard, switch view to plain text mode 

    How do I specify an absolute path? Is this done in

    Qt Code:
    1. db.setDatabaseName("testdb");
    To copy to clipboard, switch view to plain text mode 
    ?

    I can't find any obvious function in:

    http://qt-project.org/doc/qt-4.8/qsqldatabase.html

    That allows one to specify the path of the .db file.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Unable to work with database

    Connecting to a database has been absolutely done to death in these forums.
    Here is what I presently have in the .pro file
    I have no idea where you got this from. Remove the lines you gave us and ensure that you do have:
    Qt Code:
    1. QT += sql
    To copy to clipboard, switch view to plain text mode 
    How do I specify an absolute path?
    For Sqlite:
    Qt Code:
    1. db.setDatabaseName("/some/full/path/to/the/test.db");
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Unable to work with database

    Thanks Chris, you were right and I didn't even process what you wrote about my query being questionable at best, looks like it's all been working all along. Many thanks and silly me .

  7. #7
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Unable to work with database

    Qt Code:
    1. QString strDBName = "D:\path to .db file where the .exe is data.db";
    2. db.setDatabaseName(strDBName);
    3. bool dbOpen = db.open(strDBName, NULL);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. query.exec("SELECT Column1, Column2, Column3, Column4, Column5, Column6, FROM tblTable WHERE (Column4='EMPTY')"); //
    2. qDebug() << query.lastError().text(); //if error executing SQL statement occurs, get error
    3.  
    4. //Selected rows based on condition
    5. while(query.next())
    6. {
    7. qDebug() << "inside while loop";
    8. int iRow = query.value(0).toInt();
    9. int iColumn = query.value(1).toInt();
    10. qDebug() << "Row = " << iRow << "Column = " << iColumn;
    11. }
    To copy to clipboard, switch view to plain text mode 

    I've got this code, and unfortunately it's not producing any results. The first qDebug with lastError(), prints " ". According to the documentation (Returns error information about the last error (if any) that occurred with this query.), not sure if the empty brackets indeed correspond to the fact that no error occured or whether we're supposed to get no output at all if no error occured but I'm assuming that no error occured.

    The second qDebug statement inside the 'while' loop nevergets executed so not sure why the program never enters the 'while' loop.

    I've tried PRAGMA:

    Qt Code:
    1. query.exec("PRAGMA database_list");
    2. //qDebug() << query.lastError().text(); //if error executing SQL statement occurs, get error
    3.  
    4. //List of currently available databases
    5. while(query.next())
    6. {
    7. int iDatabaseConnection = query.value(2).toInt();
    8. QString sDatabaseSecondColumn = query.value(2).toString();
    9. QString sDatabaseFileName = query.value(2).toString();
    10. qDebug() << "Database connection " << iDatabaseConnection << "Database file name = " << sDatabaseFileName << "Second column = " << sDatabaseSecondColumn;
    11. }
    To copy to clipboard, switch view to plain text mode 

    and the result was:

    Database connection 0 Database file name = "D:\path to .db file where the .exe is data.db" Second column = "D:\path to .db file where the .exe is data.db"

    So the PRAGMA query enters the 'while' loop but the other does not.

  8. #8
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Unable to work with database

    I edited the code a litte:
    Qt Code:
    1. qDebug() << "Database sequence " << iDatabaseSequence << "Database name = " << sDatabaseName << "File associated = " << sDatabaseFileName;
    To copy to clipboard, switch view to plain text mode 

    Now it prints out:

    Database sequence 0 Database name = "main" File associated = "D:\path to .db file where the .exe is data.db"

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Unable to work with database

    Your query as posted is not valid SQL and should be returning an error.

    If the query was corrected in the obvious way and returns no rows, i.e. the while() loop is never entered, that would be because there are no rows in that table with 'EMPTY' in column4. This is correct behaviour.

  10. #10
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Unable to work with database

    Turns out, everything is working fine, I think my database is corrupt (or I stuffed up access to sqlite conversion), if I search for WHERE column4 = 'SOMETHING', it works, and indeed, if I just pull all the data that it can without the where statement, it only pulls the the SOMETHINGs, so not quite sure where the database got corrupted but at least it pretty much solved.

Similar Threads

  1. Replies: 0
    Last Post: 28th June 2013, 11:52
  2. Mysql unknown database, QMYSQL unable to connect
    By lixo1 in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 21:39
  3. Replies: 9
    Last Post: 20th May 2010, 09:55
  4. ODBC Oracle. Unable to connect to database
    By egil in forum Qt Programming
    Replies: 2
    Last Post: 17th October 2008, 13:46
  5. which ide/editor/database work best with qt4?
    By kkool84 in forum Installation and Deployment
    Replies: 17
    Last Post: 24th August 2006, 06:45

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.