[Solved] Inserting lineEdit contents into database
-
How can I supply the line edit text as an argument to a QSqlQuery command, that is I have the following:
QLineEdit *nameLineEdit = new QLineEdit;
QString name(nameLineEdit->text());
...
QSqlQuery query("INSERT INTO Persons (firstname) VALUES (...); //here I want to input the text from nameLineEdit
My intention is to insert the line edit contents into a database. -
Probably a bit like this:
@
QString str = QString ("INSERT INTO Persons (firstname) VALUES (" )+nameLineEdit->text()+QString(")");
QSqlQuery query(str); //here I want to input the text
@
Note: just typed. You need to check.
There are certainly more ways to achieve the same result. You may also assemble the text directly in the query contructor. -
Thank you for the suggestions.
I am able to insert the values using Skyrim suggestion, i.e supplying nameLineEdit->text() as an argument to bindValue() function.
The problem I have is putting nameLineEdit->text() as part of the query, such as in DELETE query, or using the WHERE clause, or INSERTing a single record, etc, as shown in the following:
@
QSqlQuery query("DELETE FROM Persons"
"WHERE firstname=nameLineEdit->text()"); //this is wrong but I hope it gives the idea of what I want to achieve
@
koahnig suggestion is somehow trying to solve it but unfortunately it did not work. -
Please be patient when asking for help. If nobody has responded within a day or two, then you might consider bumping your request. In the mean time, remember that we are all volunteering our time and are not here to give answers on demand.
Please see http://www.catb.org/~esr/faqs/smart-questions.html
-
ok, in fact it is simple.
for example
@
QSqlQuery del(QString("delete from Persons where Persons.firstName = '%1' ")
.arg(ui.textEdit->text()));
del.exec();
@On the same principle can be constructed of any request.
The example updates:
@
QSqlQuery up(QString("update Persons set firstName = '%1', secondName = '%2' where Persons.id = '%3' ")
.arg(ui.textEdit->text())
.arg(ui.textEdit2->text())
.arg(ID));
up.exec();
@sorry for the delay
good luck