writing to mysql table



  • 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.



  • @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?



  • 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')");
    

  • Lifetime Qt Champion

    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.



  • @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


  • Qt Champions 2017

    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
    

 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.