Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved]QSqlQuery does not work

    General and Desktop
    2
    8
    1472
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      AmirHammoutene 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

      1 Reply Last reply Reply Quote 0
      • B
        BlastDV 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.

        (8) Just live your life blind like me (8)

        1 Reply Last reply Reply Quote 0
        • B
          BlastDV 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().

          (8) Just live your life blind like me (8)

          1 Reply Last reply Reply Quote 0
          • A
            AmirHammoutene last edited by

            it does not work

            1 Reply Last reply Reply Quote 0
            • B
              BlastDV 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?

              (8) Just live your life blind like me (8)

              1 Reply Last reply Reply Quote 0
              • A
                AmirHammoutene last edited by

                last one, no changes in database.
                no error in the console

                1 Reply Last reply Reply Quote 0
                • A
                  AmirHammoutene 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

                  1 Reply Last reply Reply Quote 0
                  • B
                    BlastDV 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.

                    (8) Just live your life blind like me (8)

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post