Results 1 to 12 of 12

Thread: QSQLITE logic error

  1. #1
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QSQLITE logic error

    Hi,

    I have a QSQLITE database and I want to insert some stuff, I'm using a prepare query and finally a exec function. But I get the message:
    SQL Logic error or missing database, unable to fetch rows
    This is the code:
    Qt Code:
    1. this->connect();
    2.  
    3. if(this->isConnected){
    4. QSqlQuery query;
    5.  
    6. query.prepare("INSERT INTO clients (`cid`, `firstName`, `lastName`, `address`, `zipCode`, `city`, `phoneNr`, `email`) "
    7. " VALUES( '', :firstName, :lastName, :address, :zipCode, :city, :phoneNr, :email)");
    8. query.bindValue(":firstName", strFirstName);
    9. query.bindValue(":lastName", strLastName);
    10. query.bindValue(":address", strAddress);
    11. query.bindValue(":zipCode", strZipCode);
    12. query.bindValue(":city", strCity);
    13. query.bindValue(":phoneNr", strPhoneNr);
    14. query.bindValue(":email", strEmail);
    15.  
    16. if(query.exec()){
    17. emit databaseChanged();
    18. return true;
    19. }else{
    20. QSqlError error = query.lastError();
    21. QString sError = error.text();
    22. QMessageBox::critical(this, tr("SQL error"), sError);
    23. return false;
    24. }
    25. this->closeConnection();
    26. }
    To copy to clipboard, switch view to plain text mode 

    Does somebody know how to solve this?

    Thx,

    Cyberboy
    Last edited by jacek; 8th February 2008 at 14:59. Reason: wrapped too long line

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSQLITE logic error

    J-P Nurmi

  3. #3
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QSQLITE logic error

    Yes I have a initialize function for the database here is de code :
    Qt Code:
    1. db::db(){
    2.  
    3. //loading the drivers
    4. database = QSqlDatabase::addDatabase ("QSQLITE");
    5. //set database name
    6. database.setDatabaseName("vredeveldOrderDatabase");
    7. //set connection flag
    8. this->isConnected = database.open();
    9. //set error
    10. QSqlError qError = database.lastError();
    11. QString sError = qError.text();
    12. if(!this->isConnected){
    13. //if isn't connected show error message
    14. QMessageBox::critical(this, tr("Database connection isn't open!"),
    15. sError,
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    And here the connect function
    Qt Code:
    1. bool db::connect(){
    2. if(this->isConnected == false){
    3. database.open();
    4. this->isConnected = true;
    5. }
    6. return true;
    7. }
    To copy to clipboard, switch view to plain text mode 

    And the code in the previous post is a function for adding clients.

    So I did call the setDatabaseName function....

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSQLITE logic error

    Print QSqlQuery::lastQuery() and try executing it with SQLite Browser. Maybe it gives a more detailed error message.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QSQLITE logic error

    Thanks for the tip, this is the error message I get
    insert into clients (`cid`, `firstName`, `lastName`, `address`, `zipCode`, `city`, `phoneNr`, `email`) values ( '', :firstName, :lastName, :address, :zipCode, :city, honeNr, :email)
    So the prepare and the bindValue isn't working properly, is there anybody familiar with this problem?

    I've found something I'm passing QString vars in the bindValue, I've changed it to QVariant but that works neither.

    Could it be that it won't work because I'm converting a QString to QVariant;

    Here's the code to get the text
    Qt Code:
    1. QString strFirstName = firstNameLine->text();
    2. QString strLastName = lastNameLine->text();
    3. QString strAddress = addressLine->text();
    4. QString strZipCode = zipCodeLine->text();
    5. QString strCity = cityLine->text();
    6. QString strPhoneNr = phoneNrLine->text();
    7. QString strEmail = emailLine->text();
    8.  
    9.  
    10. if(database->addClient(strFirstName, strLastName, strAddress, strZipCode, strCity, strPhoneNr, strEmail)){
    11. clientDialog->accept();
    12. }else{
    13. QMessageBox::critical(clientDialog, "Error", "Er is een fout opgetreden bij het invoeren van een klant, neem contact op met de ontwikkelaar!");
    14. }
    To copy to clipboard, switch view to plain text mode 

    and here's the code of the function
    Qt Code:
    1. bool db::addClient(QVariant strFirstName, QVariant strLastName, QVariant strAddress, QVariant strZipCode, QVariant strCity, QVariant strPhoneNr = "", QVariant strEmail = ""){
    To copy to clipboard, switch view to plain text mode 
    Last edited by cyberboy; 8th February 2008 at 17:56. Reason: add information

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSQLITE logic error

    Sorry, my bad. Seems that neither QSqlQuery::lastQuery() nor QSqlQuery::executedQuery() will print bound values. You can print them with QSqlQuery::boundValues() though:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtSql>
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    8. db.setDatabaseName(":memory:");
    9. if (!db.open())
    10. {
    11. qWarning() << "cannot open database";
    12. return -1;
    13. }
    14.  
    15. QSqlQuery query;
    16. query.exec("create table person (id int primary key, firstname varchar(20), lastname varchar(20))");
    17.  
    18. query.prepare("insert into person (`id`, `firstname`, `lastname`) values (:id, :firstname, :lastname)");
    19. query.bindValue(":id", 101);
    20. query.bindValue(":firstname", "Danny");
    21. query.bindValue(":lastname", "Young");
    22.  
    23. qDebug() << query.exec()
    24. << query.executedQuery()
    25. << query.boundValues();
    26. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. #7
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QSQLITE logic error

    Damn this is weird!

    Here is the piece of code that sends the error messages:
    Qt Code:
    1. //Woops there went something terrible wrong, popup the message box
    2. QSqlError error = query.lastQuery();
    3. QString sError = error.text();
    4. QMessageBox::critical(this, tr("SQL error"), sError);
    5. QString ssError = query.executedQuery();
    6. QMessageBox::critical(this, tr("SQL error"), ssError);
    7. QMapIterator<QString, QVariant> i(query.boundValues());
    8. while (i.hasNext()) {
    9. i.next();
    10. QMessageBox::warning(this, tr("SQL Warning"), i.value().toString().toAscii().data());
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    And when I open the QDialog box and fill in this information
    1
    2
    3
    4
    5
    6
    7
    The SQL Warning message box displays it in this order
    3
    5
    7
    1
    2
    6
    4
    I don't know if that's a problem?

    And the query.executedQuery()
    returns this:
    insert into clients (`cid`, `firstName`, `lastName`, `address`, `zipCode`, `city`, `phoneNr`, `email`) values ( '', ? , ? , ? , ? , ? , ? , ? )
    So he didn't bind a value in the query?

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSQLITE logic error

    Would you mind opening the database to SQLite Browser and executing the query?

    Quote Originally Posted by cyberboy View Post
    So he didn't bind a value in the query?
    Am I talking to a wall? I just told you that in my previous message, didn't I?
    J-P Nurmi

  9. #9
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QSQLITE logic error

    I'm really sorry, I did put the query in the SQLite browser.
    And it said no error

    But I discovered something!
    First I made a full query and that one worked.
    Second I made a prepare query but I didn't take the values from the QDialog but I defined my own QVariant var = "values"; and that one works too

    So the problem is, at least I think, that the QString isn't converted well to a QVariant.
    Last edited by cyberboy; 9th February 2008 at 13:21.

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSQLITE logic error

    Output bound values like my example shows and you'll get more detailed information.
    J-P Nurmi

  11. #11
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QSQLITE logic error

    this may sound really stupid, but when I do a qDebug() I don't know where it dumps the output (yeah I'm a beginner)

  12. #12
    Join Date
    Jan 2008
    Location
    Warsaw, Poland
    Posts
    26
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default Re: QSQLITE logic error

    Quote Originally Posted by cyberboy View Post
    this may sound really stupid, but when I do a qDebug() I don't know where it dumps the output (yeah I'm a beginner)
    I'm also a beginner, but I know that using documentation is crucial: qDebug(). If you don't know what is stderr (is it possible for programmer?), try Wikipedia: stderr.

Similar Threads

  1. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  2. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 03:49
  3. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 13:19
  4. Qt-x11-commercial-src-4.2.0-snapshot-20060824 error
    By DevObject in forum Installation and Deployment
    Replies: 4
    Last Post: 25th August 2006, 00:31
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13:52

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.