Did you call QSqlDatabase::setDatabaseName()?
Yes I have a initialize function for the database here is de code :
Qt Code:
db::db(){ //loading the drivers //set database name database.setDatabaseName("vredeveldOrderDatabase"); //set connection flag this->isConnected = database.open(); //set error if(!this->isConnected){ //if isn't connected show error message sError, } }To copy to clipboard, switch view to plain text mode
And here the connect function
Qt Code:
bool db::connect(){ if(this->isConnected == false){ database.open(); this->isConnected = true; } return true; }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....
Print QSqlQuery::lastQuery() and try executing it with SQLite Browser. Maybe it gives a more detailed error message.
J-P Nurmi
Thanks for the tip, this is the error message I get
So the prepare and the bindValue isn't working properly, is there anybody familiar with this problem?insert into clients (`cid`, `firstName`, `lastName`, `address`, `zipCode`, `city`, `phoneNr`, `email`) values ( '', :firstName, :lastName, :address, :zipCode, :city,honeNr, :email)
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:
if(database->addClient(strFirstName, strLastName, strAddress, strZipCode, strCity, strPhoneNr, strEmail)){ clientDialog->accept(); }else{ QMessageBox::critical(clientDialog, "Error", "Er is een fout opgetreden bij het invoeren van een klant, neem contact op met de ontwikkelaar!"); }To copy to clipboard, switch view to plain text mode
and here's the code of the function
Last edited by cyberboy; 8th February 2008 at 16:56. Reason: add information
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:
#include <QtCore> #include <QtSql> int main(int argc, char* argv[]) { db.setDatabaseName(":memory:"); if (!db.open()) { qWarning() << "cannot open database"; return -1; } QSqlQuery query; query.exec("create table person (id int primary key, firstname varchar(20), lastname varchar(20))"); query.prepare("insert into person (`id`, `firstname`, `lastname`) values (:id, :firstname, :lastname)"); query.bindValue(":id", 101); query.bindValue(":firstname", "Danny"); query.bindValue(":lastname", "Young"); qDebug() << query.exec() << query.executedQuery() << query.boundValues(); }To copy to clipboard, switch view to plain text mode
J-P Nurmi
Damn this is weird!
Here is the piece of code that sends the error messages:
Qt Code:
//Woops there went something terrible wrong, popup the message box QMapIterator<QString, QVariant> i(query.boundValues()); while (i.hasNext()) { i.next(); }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:
So he didn't bind a value in the query?insert into clients (`cid`, `firstName`, `lastName`, `address`, `zipCode`, `city`, `phoneNr`, `email`) values ( '', ? , ? , ? , ? , ? , ? , ? )
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 12:21.
Output bound values like my example shows and you'll get more detailed information.
J-P Nurmi
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)
Bookmarks