Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]QSqlQuery does not work
QtWS25 Last Chance

[Solved]QSqlQuery does not work

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 1.8k Views
  • 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 Offline
    A Offline
    AmirHammoutene
    wrote on last edited by
    #1

    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
    0
    • B Offline
      B Offline
      BlastDV
      wrote on last edited by
      #2

      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
      0
      • B Offline
        B Offline
        BlastDV
        wrote on last edited by
        #3

        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
        0
        • A Offline
          A Offline
          AmirHammoutene
          wrote on last edited by
          #4

          it does not work

          1 Reply Last reply
          0
          • B Offline
            B Offline
            BlastDV
            wrote on last edited by
            #5

            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
            0
            • A Offline
              A Offline
              AmirHammoutene
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                AmirHammoutene
                wrote on last edited by
                #7

                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
                0
                • B Offline
                  B Offline
                  BlastDV
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved