[Qt-interest] QSqlQuery used with bindValue, returns QSqlError(-1, "", "") last error when query.exec() executes

S. Aguinaga sa_jr at yahoo.com
Thu Aug 20 14:29:46 CEST 2009



Hello!

If i use the following code in a Slot callback I am able to read from mysql database:

Qt Code:
1
2
3
4
5
6
 QSqlQuery query;
    query.exec("SELECT LastName FROM Experimenters WHERE ExpUserName='sag686'");
    while (query.next()) {
        QString exper = query.value(0).toString();
        qDebug() << exper;
    } 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.  Do I need to "check that the DB connection" is OK, before trying to execute an 'INSERT' ?  As my original connection to the db was set back in my main.cpp.


Qt Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 QSqlQuery query;
    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)");
 
    query.bindValue( ":ExpUserName", expUserIDEdit->displayText() );
    query.bindValue( ":Password", passwordEdit->displayText() );
    query.bindValue( ":FirstName", firstnameEdit->displayText() );
    query.bindValue( ":LastName", lastnameEdit->displayText() );
    query.bindValue( ":Email", emailEdit->displayText() );
    query.bindValue( ":Phone", phoneEdit->displayText() );
    query.bindValue( ":CreateDate", QDate::currentDate().toString("dd,mm,yyyy") );
    query.bindValue( ":LastModified", QDate::currentDate().toString("dd,mm,yyyy") ) ;
    query.bindValue( ":HarpGroup", harpGroupCB->currentText() );
    query.bindValue( ":isActive", 1);
    if(!query.exec())
        qDebug() << "> Query exec() error." << query.lastError();
    else
        qDebug() << ">Query exec() success.";  


      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090820/a250f404/attachment.html 


More information about the Qt-interest-old mailing list