PDA

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



windsword
20th August 2009, 14:19
Hello!

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

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.


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.";

windsword
21st August 2009, 15:53
I also tried other combinations:

QSqlQuery query;
query.prepare(QLatin1String( "INSERT INTO Clients (ExpUserName, Password, FirstName, LastName,Email, Phone, CreateDate, LastModified, HarpGroup, isActive) VALUES (?,?,?,?,?,?,?,?,?,?)"));
....
if( !query.exec() )
qDebug() << "> Query exec() error." << query.lastError().type();
else
qDebug() << ">Query exec() success.";


The error type is 0.

gboelter
22nd August 2009, 08:29
Normally a SQL Server has very good log-files.

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

windsword
25th August 2009, 14:03
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!