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


  • Moderators

    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.



  • 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();
    @



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


  • Moderators

    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



  • My apologies for my impatience. Thank you for the link, mlong; I also didnt know anything about bumping.
    And thanks Skyrim, it worked!


  • Moderators

    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!



  • You can use prepared queries everywhere :

    @
    QSqlQuery q;
    q.prepare("DELETE FROM Persons WHERE firstName=:firstName");
    q.bindValue(":firstName", nameLineEdit->text());
    if (q.exec()) {
    }
    @


Log in to reply
 

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