Answer the question
In order to leave comments, you need to log in
What is the error and how to fix it when connecting and getting data from the database through QT?
I connect the database to the program, when I try to click on the button to get data from the database, some error pops up:
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
please tell me why it occurs and how to fix it?
main.cpp code
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QtSql/QSqlDatabase"
#include "QSqlQuery"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
// database.setDatabaseName("C:Users/NikaB/Desktop/blabla/TestSQL/database.db3");
// if (!database.open())
// ui->label->setText("open the database");
// else
// ui->label->setText("connected...");
}
void MainWindow::on_pushButton_clicked()
{
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("baza/database.db3");
db.open();
//Осуществляем запрос
QSqlQuery query;
query.exec("SELECT _id, Name, Age FROM People");
//Выводим значения из запроса
while (query.next())
{
QString _id = query.value(0).toString();
QString Name = query.value(1).toString();
QString Age = query.value(2).toString();
ui->textEdit->insertPlainText(_id+" "+Name+" "+Age+"\n");
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question