[Solved]QSqlQuery does not work
-
wrote on 14 Jul 2014, 17:06 last edited by
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
-
wrote on 14 Jul 2014, 19:43 last edited by
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.
-
wrote on 14 Jul 2014, 19:50 last edited by
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().
-
wrote on 14 Jul 2014, 21:57 last edited by
it does not work
-
wrote on 14 Jul 2014, 22:06 last edited by
Please can you be a little be more specific?
Is it prompting an error message? Are you getting database warnings? Is the .exec() returning and error? Is it that you don't see any change on the database?
-
wrote on 14 Jul 2014, 22:20 last edited by
last one, no changes in database.
no error in the console -
wrote on 14 Jul 2014, 23:39 last edited by
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 -
wrote on 15 Jul 2014, 02:00 last edited by
I see. Don't worry, this is what the forum is about.
Please mark the thread as SOLVED by adding "[Solved]" before the title.
4/8