Answer the question
In order to leave comments, you need to log in
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
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();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question