[Solved] Inserting lineEdit contents into database
-
wrote on 2 Mar 2012, 13:45 last edited by
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. -
wrote on 2 Mar 2012, 13:57 last edited by
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. -
wrote on 2 Mar 2012, 18:44 last edited by
Hi
@
QSqlQuery query("insert into Persons (firstname) values (?)");
// if database is SQLite then QSqlQuery query("insert into Persons (firstname) values (:firstname)");
query.bindValue(0, nameLineEdit->text());
query.exec();
@ -
wrote on 12 Mar 2012, 10:31 last edited by
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. -
wrote on 12 Mar 2012, 13:39 last edited by
Please help...
-
wrote on 12 Mar 2012, 15:24 last edited by
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
-
wrote on 12 Mar 2012, 21:08 last edited by
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 -
wrote on 14 Mar 2012, 12:44 last edited by
My apologies for my impatience. Thank you for the link, mlong; I also didnt know anything about bumping.
And thanks Skyrim, it worked! -
wrote on 14 Mar 2012, 14:52 last edited by
No problem! Glad you've got it working! One more thing, if your issue is resolved, please edit your original post and add [Solved] to the beginning of the title. Thanks!
-
wrote on 14 Mar 2012, 15:20 last edited by
You can use prepared queries everywhere :
@
QSqlQuery q;
q.prepare("DELETE FROM Persons WHERE firstName=:firstName");
q.bindValue(":firstName", nameLineEdit->text());
if (q.exec()) {
}
@