Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Inserting lineEdit contents into database
QtWS25 Last Chance

[Solved] Inserting lineEdit contents into database

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 7.4k Views
  • 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 Offline
    M Offline
    manaila
    wrote on last edited by
    #1

    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
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        Skyrim
        wrote on last edited by
        #3

        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
        0
        • M Offline
          M Offline
          manaila
          wrote on last edited by
          #4

          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
          0
          • M Offline
            M Offline
            manaila
            wrote on last edited by
            #5

            Please help...

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              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
              0
              • S Offline
                S Offline
                Skyrim
                wrote on last edited by
                #7

                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
                0
                • M Offline
                  M Offline
                  manaila
                  wrote on last edited by
                  #8

                  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
                  0
                  • M Offline
                    M Offline
                    mlong
                    wrote on last edited by
                    #9

                    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
                    0
                    • O Offline
                      O Offline
                      octal
                      wrote on last edited by
                      #10

                      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
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved