Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. writing to mysql table
QtWS25 Last Chance

writing to mysql table

Scheduled Pinned Locked Moved Unsolved Brainstorm
6 Posts 4 Posters 1.4k 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.
  • J Offline
    J Offline
    jason bourne
    wrote on last edited by jason bourne
    #1

    Hi, so i have a mysql database with a table containing the following columns:
    username,password,hardware id

    my statement looks something like this:
    if (mysqlquery.value("HWID").toString().toUtf8().constData() == null)
    mysqlquery.exec("INSERT INTO members (HWID) VALUES ('test')");

    this is inserting correctly, however into a completely new column, not into the users.

    JonBJ 1 Reply Last reply
    0
    • J jason bourne

      Hi, so i have a mysql database with a table containing the following columns:
      username,password,hardware id

      my statement looks something like this:
      if (mysqlquery.value("HWID").toString().toUtf8().constData() == null)
      mysqlquery.exec("INSERT INTO members (HWID) VALUES ('test')");

      this is inserting correctly, however into a completely new column, not into the users.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @jason-bourne

      however into a completely new column, not into the users

      ?

      INSERT INTO members (HWID) VALUES ('test')

      That statement will insert a row into table named members. It will set the value in column named HWID to 'test'. Other columns will be set to a default value or NULL . What else is there to say?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jason bourne
        wrote on last edited by jason bourne
        #3

        It's making a completely new column though

        my current check is if (username and pass is correct)
        if hardwareid column is empty then set hardware id to users hardware id, but it's just making a new row with a new column, not inserting the hardwareid into the users column understand?

        http://prntscr.com/lgphgq

        if (ui->lineusername->text() == mysqlquery.value("Username").toString().toUtf8().constData() && ui->linepassword->text() == mysqlquery.value("Password").toString().toUtf8().constData())
                   {
                       if (mysqlquery.value("HWID").toString().toUtf8().constData() == null)
                           mysqlquery.exec("INSERT INTO members (HWID) VALUES ('test')");
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Because that's exactly what your asking your database to do. If you want to modify a record use UPDATE.

          By the way, there's no need for all these conversions. For example: mysqlquery.value("HWID").isNull().

          On a side note, your first if makes it clear that you are storing your user password in clear text in the database which is a bit security issue. You should change that.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Because that's exactly what your asking your database to do. If you want to modify a record use UPDATE.

            By the way, there's no need for all these conversions. For example: mysqlquery.value("HWID").isNull().

            On a side note, your first if makes it clear that you are storing your user password in clear text in the database which is a bit security issue. You should change that.

            J Offline
            J Offline
            jason bourne
            wrote on last edited by jason bourne
            #5

            @SGaist

            Perfect, liking the update query, but how can i pass in a string from QT to be updated? not quite sure how to do it with a query

            Edit: did it using bind queries tysm

            kshegunovK 1 Reply Last reply
            0
            • J jason bourne

              @SGaist

              Perfect, liking the update query, but how can i pass in a string from QT to be updated? not quite sure how to do it with a query

              Edit: did it using bind queries tysm

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              There are examples in the documentation you should explore. On possibility is:

              QString myStringArgument = "whatever it is";
              
              QSqlQuery query;
              if (!query.prepare("INSERT INTO tableName (columnName) VALUES (?)"))
                   ; // Handle error
              
              query.addBindValue(myStringArgument);
              if (!query.exec())
                  ; // Handle error
              

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              1

              • Login

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