Results 1 to 4 of 4

Thread: QSqlQuery to insert data to db returns lastError() = QSqlError(-1, "", "")

  1. #1
    Join Date
    Nov 2008
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default QSqlQuery to insert data to db returns lastError() = QSqlError(-1, "", "")

    Hello!

    If i use the following code in a Slot callback I am able to read from mysql database:
    Qt Code:
    1. QSqlQuery query;
    2. query.exec("SELECT LastName FROM Experimenters WHERE ExpUserName='sag686'");
    3. while (query.next()) {
    4. QString exper = query.value(0).toString();
    5. qDebug() << exper;
    6. }
    To copy to clipboard, switch view to plain text mode 

    But if I use the following to Write to the database, I get error, any hints into what I might be doing wrong? The strings come from a Form's LineEdit fields.
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare( "INSERT INTO Clients (ExpUserName, Password, FirstName, LastName,Email, Phone, CreateDate, LastModified, HarpGroup, isActive) VALUES (:ExpUserName,:Password, :FirstName, :LastName, :Email, :Phone, :CreateDate, :LastModified, :HarpGroup, :isActive)");
    3.  
    4. query.bindValue( ":ExpUserName", expUserIDEdit->displayText() );
    5. query.bindValue( ":Password", passwordEdit->displayText() );
    6. query.bindValue( ":FirstName", firstnameEdit->displayText() );
    7. query.bindValue( ":LastName", lastnameEdit->displayText() );
    8. query.bindValue( ":Email", emailEdit->displayText() );
    9. query.bindValue( ":Phone", phoneEdit->displayText() );
    10. query.bindValue( ":CreateDate", QDate::currentDate().toString("dd,mm,yyyy") );
    11. query.bindValue( ":LastModified", QDate::currentDate().toString("dd,mm,yyyy") ) ;
    12. query.bindValue( ":HarpGroup", harpGroupCB->currentText() );
    13. query.bindValue( ":isActive", 1);
    14. if(!query.exec())
    15. qDebug() << "> Query exec() error." << query.lastError();
    16. else
    17. qDebug() << ">Query exec() success.";
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2008
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: QSqlQuery to insert data to db returns lastError() = QSqlError(-1, "", "")

    I also tried other combinations:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare(QLatin1String( "INSERT INTO Clients (ExpUserName, Password, FirstName, LastName,Email, Phone, CreateDate, LastModified, HarpGroup, isActive) VALUES (?,?,?,?,?,?,?,?,?,?)"));
    3. ....
    4. if( !query.exec() )
    5. qDebug() << "> Query exec() error." << query.lastError().type();
    6. else
    7. qDebug() << ">Query exec() success.";
    To copy to clipboard, switch view to plain text mode 

    The error type is 0.

  3. #3
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQuery to insert data to db returns lastError() = QSqlError(-1, "", "")

    Normally a SQL Server has very good log-files.

    You have checked the query-log already ...?

  4. #4
    Join Date
    Nov 2008
    Posts
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: QSqlQuery to insert data to db returns lastError() = QSqlError(-1, "", "")

    I realized that I was trying to write data to the wrong table, so that's probably why the lastError was undefined. Sometimes, the answer is so simple... My bad!

Tags for this Thread

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.