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.7k 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.
  • Lazar1L Offline
    Lazar1L Offline
    Lazar1
    wrote on 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
    • miclandM Offline
      miclandM Offline
      micland
      wrote on 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
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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

        miclandM 1 Reply Last reply
        7
        • mrjjM mrjj

          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

          miclandM Offline
          miclandM Offline
          micland
          wrote on 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
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on 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
            • Lazar1L Offline
              Lazar1L Offline
              Lazar1
              wrote on 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

              • Login

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