[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");
    QMessageBox::information(this,"Message","Not connected...");

        QSqlQuery query;
        query.exec(QString("INSERT INTO `mmr` VALUES (NULL,'%1','%2','%3','%4')")
        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

  • 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?

  • 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

  • I see. Don't worry, this is what the forum is about.

    Please mark the thread as SOLVED by adding "[Solved]" before the title.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.