writing to mysql table
-
wrote on 10 Nov 2018, 14:13 last edited by jason bourne 11 Oct 2018, 14:21
Hi, so i have a mysql database with a table containing the following columns:
username,password,hardware idmy 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.
-
Hi, so i have a mysql database with a table containing the following columns:
username,password,hardware idmy 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.
wrote on 10 Nov 2018, 15:54 last edited by JonB 11 Oct 2018, 15:56however 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 namedHWID
to'test'
. Other columns will be set to a default value orNULL
. What else is there to say? -
wrote on 10 Nov 2018, 16:43 last edited by jason bourne 11 Oct 2018, 16:46
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?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')");
-
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.
-
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.
wrote on 10 Nov 2018, 20:19 last edited by jason bourne 11 Oct 2018, 20:25Perfect, 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
-
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
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
2/6