I don't understand what you mean. The code is a little more complex. So basically the code is something like that:
int record = 0;
//inițializări
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
//conectare la baza de date
db.setDatabaseName("conturi.db");
if (!db.open())
{
messageBox ->warning(this, tr("Database Error"), db.lastError().text());
exit(0);
}
else
{
//adăugare listă conturi din baza de date
query ->exec("SELECT * FROM cont");
if (!query ->isActive())
{
messageBox ->warning(this, tr("Database Error"), query ->lastError().text());
exit(0);
}
else
{
while (query ->next())
{
ui ->comboBox ->addItem(query ->value(3).toString());
}
}
}
}
//curățare tot
MainWindow::~MainWindow()
{
db.close();
delete ui;
}
QSqlDatabase db;
int record = 0;
//inițializări
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
QSqlQuery *query;
QMessageBox *messageBox = new QMessageBox;
ui->setupUi(this);
//conectare la baza de date
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("conturi.db");
if (!db.open())
{
messageBox ->warning(this, tr("Database Error"), db.lastError().text());
exit(0);
}
else
{
//adăugare listă conturi din baza de date
query = new QSqlQuery(db);
query ->exec("SELECT * FROM cont");
if (!query ->isActive())
{
messageBox ->warning(this, tr("Database Error"), query ->lastError().text());
exit(0);
}
else
{
while (query ->next())
{
ui ->comboBox ->addItem(query ->value(3).toString());
}
}
}
}
//curățare tot
MainWindow::~MainWindow()
{
db.close();
delete ui;
}
To copy to clipboard, switch view to plain text mode
And of course there are other line codes for other operations on the database. So what I changed was defining db as a pointer and made changes everywhere from "." to "->" and from "db" to "*db".
Later edit: So using the method addDatabase I didn't assigned the driver QSQLITE to the variable db? I must use another function or something like that?
Added after 15 minutes:
Ok, so I found which was the solution: *db = QSqlDatabase::addDatabase("QSQLITE");
Bookmarks