Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] Inserting lineEdit contents into database

    General and Desktop
    5
    10
    6809
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      manaila 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.

      1 Reply Last reply Reply Quote 0
      • K
        koahnig 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.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply Reply Quote 0
        • S
          Skyrim 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();
          @

          1 Reply Last reply Reply Quote 0
          • M
            manaila 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.

            1 Reply Last reply Reply Quote 0
            • M
              manaila last edited by

              Please help...

              1 Reply Last reply Reply Quote 0
              • M
                mlong 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

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply Reply Quote 0
                • S
                  Skyrim 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

                  1 Reply Last reply Reply Quote 0
                  • M
                    manaila 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!

                    1 Reply Last reply Reply Quote 0
                    • M
                      mlong 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!

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                      1 Reply Last reply Reply Quote 0
                      • O
                        octal 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()) {
                        }
                        @

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post