N
N
nika092017-05-01 09:08:32
SQL
nika09, 2017-05-01 09:08:32

How to send data to DB from LineEdit?

there is a form with LineEdit in which the text is entered by the user, I read the value from it and want to send it to the database at the click of a button, but for some reason the data is not sent, please tell me what's the trouble, how to implement reading from LineEdit and sending values ​​to the Table ??? ? The connection to the database is up and running.
main.cpp code

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QtSql/QSqlDatabase"
#include "QSqlQuery"
#include <QDebug>
#include <QSqlError>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(nashSlot()));

}
void MainWindow::on_pushButton_clicked(){

            QSqlQuery query = QSqlQuery(db);
            query.prepare("INSERT INTO People (Name) VALUES (:Name);"); //хочу вставить записать из LineEdit в таблицу People в столбец Name.
            query.bindValue (":Name", "str1"); // str1 переменная в которую записано введенное значение из LineEdit.
        }

   void MainWindow::nashSlot()
{
    QString str1 = ui->lineEdit->text(); // получаем строку из первого QLineEdit 
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nika09, 2017-05-01
@nika09

void MainWindow::on_pushButton_clicked(){
QSqlQuery query = QSqlQuery(db);
query.prepare("INSERT INTO People (Name, Age)"
"VALUES (?, ?)");
QString str1 = ui->lineEdit->text();
QString str2 = ui->lineEdit_2->text();
query.addBindValue(str1);
query.addBindValue(str2);
query.exec();
}

J
Jacob E, 2017-05-01
@Zifix

Read any C++ textbook, because neither LineEdit nor the database is at fault here. The banal ignorance of the language and the unwillingness to learn it are to blame.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question