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. Insert data from variables to Database
Forum Updated to NodeBB v4.3 + New Features

Insert data from variables to Database

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.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.
  • L Offline
    L Offline
    Lazar1
    wrote on 13 Jun 2016, 09:40 last edited by
    #1

    Good morning.

    I am working on a project and while i am able to send numbers to my MYSQL database with this code:

    QSqlQuery query;
    query.prepare("INSERT INTO vathmoi (V_1) "
    "VALUES (10)");
    query.exec(); // this runs it
    

    i can not send data using that code

    QString b;
    b=10;
    QSqlQuery query;
    query.prepare("INSERT INTO vathmoi (V_1) "
    "VALUES (b)");
    query.exec(); // this runs it
    

    So i dont know how to handle that isue cause in the project i am developing all that wich are going to be stored in the db are in variables.

    Thank you in advance.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      micland
      wrote on 13 Jun 2016, 09:49 last edited by micland
      #2

      You can reference variables in your quere using bindValue(...):

      query.prepare("INSERT INTO vathmoi (V_1) VALUES (:myValue)");
      query.bindValue(":myValue", b);
      query.exec();
      // ....
      

      See also these examples: http://doc.qt.io/qt-5/sql-sqlstatements.html#inserting-updating-and-deleting-records

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 13 Jun 2016, 09:52 last edited by mrjj
        #3

        Hi and good morning :)
        Your syntax still looks wrong. ( use syntax as @micland shows)
        and please also use
        if(!query.exec()) {
        qDebug() << "SQL Statement Error" << query.lastError();
        }
        as @the_
        mentions in
        https://forum.qt.io/topic/68149/connect-mysql-database-to-qt-app/4

        M 1 Reply Last reply 13 Jun 2016, 10:08
        7
        • M mrjj
          13 Jun 2016, 09:52

          Hi and good morning :)
          Your syntax still looks wrong. ( use syntax as @micland shows)
          and please also use
          if(!query.exec()) {
          qDebug() << "SQL Statement Error" << query.lastError();
          }
          as @the_
          mentions in
          https://forum.qt.io/topic/68149/connect-mysql-database-to-qt-app/4

          M Offline
          M Offline
          micland
          wrote on 13 Jun 2016, 10:08 last edited by
          #4

          @Lazar1
          Aye! right - as @mrjj said never drop the error handling because you can't be sure that your backend is (still) up and running and works as expected... a useful error handling may prevent a lot of debugging.

          1 Reply Last reply
          3
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 13 Jun 2016, 10:09 last edited by
            #5

            Just a note:
            QString b;
            b=10;
            qDebug() << b;

            Does NOT give u "10" but ascii 10
            b=65
            would give u "A"

            QString b;
            b=QString::number(10);

            Give you "10".

            1 Reply Last reply
            7
            • L Offline
              L Offline
              Lazar1
              wrote on 13 Jun 2016, 11:29 last edited by
              #6

              Thank you all for your quick and correct ansewrs everything works fine now and i have learned a lot about debuging through your comments and i ll be sure to use it well in my programms. I will now close the topic again thanks for everything

              1 Reply Last reply
              2

              1/6

              13 Jun 2016, 09:40

              • Login

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