[Solved]QSqlQuery does not work
-
Hi everyone,
I try to add to my database a row but it does not work. My database is host by alwaysdata, I use Qt Creator to develop my program (which print no error) and MySql for viewing the database
via MySql Query Browser I entered :
@INSERT INTO
mmr
VALUES (NULL,'musictest','albumtest','timetest','datetest');@
it works but in my program that code does not work :
@
void MainWindow::b_clicked(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("mysql1.alwaysdata.com");
db.setDatabaseName("mymusicrecognition_mmr");
db.setUserName("xxx");
db.setPassword("yyyy");
if(!db.open())
{
QMessageBox::information(this,"Message","Not connected...");
}
else{QSqlQuery query; query.exec(QString("INSERT INTO `mmr` VALUES (NULL,'%1','%2','%3','%4')") .arg("musictest").arg("albumtest").arg("timetest").arg("datetest")); QMessageBox::information(this,"Message","Connected !!!"); }
}@
I have the message box "Connected !!!"This must be a beginner mistake
-
Hi, why do you say it doesn't work? You don't see the record on your table? Or does the application report an error message with the query?
By the way, I think you don't need the ' ' around the table name. I do INSERT queries like this:
@
InsertQuery->exec(QString("INSERT INTO actividad (usuario, tiempo, actividad) VALUES (")+QString("'")+UserID+QString("', '")+QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")+QString("', ")+QString("'")+evento+QString("')"))
@And they work!
Of course, the ".arg()" is ok, it depends on what you are trying to insert.
-
Reading your query again I found something weird beside what I told you about the ' ' around the table name.
Why are you using " " inside arg() ? If you are trying to insert string data (that would be varchar, I think), you should write your query like mine. On the other hand, if musictest, albumtest, timetest and datetest are other kinds of data like, let's say integers, you must not use " " for them inside arg().
-
it does not work
-
last one, no changes in database.
no error in the console -
ok i'm all wrong, I just discovered what is sqlite and I want to use MySql with database hosted on alwaysdata
thank u for your help anyway