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]Inserting values in QMysql
Forum Updated to NodeBB v4.3 + New Features

[solved]Inserting values in QMysql

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.6k Views 2 Watching
  • 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.
  • K Offline
    K Offline
    killerman
    wrote on last edited by killerman
    #1

    Hi. I've created a new Database called "people" and a new table in it called "table" with three columns (id, FirstName, LastName) but I have problem when i try to insert data in it.
    The table already has 3 records;

    Here is my programm :

    #include <QCoreApplication>
    #include <QDebug>
    #include <QtSql>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("people");
    db.setUserName("root");
    db.setPassword("");
    
    if(db.open()){
        qDebug() << "Opened";
    
        QString sQuery = "INSERT INTO table(id, FirstName, LastName) VALUES (:id, :FirstName, :LastName)";
    
        QSqlQuery qry;
        qry.prepare(sQuery);
        qry.bindValue(":id", 4);
        qry.bindValue(":FirstName", "Heather");
        qry.bindValue(":LastName", "Heather");
    
        qry.exec();
    
        qDebug() << "Closing";
        db.close();
    }
    else{
        qDebug() << "Whoops! ERRROR";
        qDebug() << db.lastError();
    }
    
    return a.exec(); 
    }
    

    It runs with no errors but nothing is added to the table...

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      @killerman said:

      qry.exec();

      You should check the returned value of QSqlQuery::prepare() and QSqlQuery::exec()

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        killerman
        wrote on last edited by
        #3

        i get false and false.

        The connection to database is good because i can read from it...

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          So it means you have error preparing the query.

          Do this

          bool result = qry.prepare(sQuery);
          if (!result) {
              qDebug() << qry.lastError();
          }
          

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            killerman
            wrote on last edited by
            #5

            QSqlError("1064", "QMYSQL3: Unable to prepare statement", "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table(id, FirstName, LastName)VALUES (?, ?, ?)' at line 1")

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by mcosta
              #6

              @killerman said:

              table

              I'm not 100% sure you can use table as table name.
              Anyway IIRC MySQL is case sensitive for table name if it runs on a case sensitive filesystem/OS (like Linux for example): check if the table name is correct

              Try to run the same query from a different MySQL client to verify the syntax

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • K Offline
                K Offline
                killerman
                wrote on last edited by
                #7

                I've made another DB and it works fine.

                "table" was the problem. Thanks a lot :)

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mcosta
                  wrote on last edited by
                  #8

                  You're welcome.

                  Remember to mark the thread as SOLVED

                  Once your problem is solved don't forget to:

                  • Mark the thread as SOLVED using the Topic Tool menu
                  • Vote up the answer(s) that helped you to solve the issue

                  You can embed images using (http://imgur.com/) or (http://postimage.org/)

                  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