Qt
Qt - Gestire i database
Ricerca…
Osservazioni
- Avrai bisogno del plugin Qt SQL corrispondente al tipo dato a
QSqlDatabase::addDatabase
- Se non si dispone del plug-in SQL richiesto, Qt ti avviserà che non è possibile trovare il driver richiesto
- Se non si dispone del plug-in SQL richiesto, sarà necessario compilare i file dal sorgente Qt
Utilizzando un database su Qt
Nel file Project.pro aggiungiamo:
CONFIG += sql
in MainWindow.h scriviamo:
#include <QMainWindow>
#include <QSql>
#include <QDebug>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
QSqlDatabase db;
};
Ora in MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QT SQL DRIVER" , "CONNECTION NAME");
db.setDatabaseName("DATABASE NAME");
if(!db.open())
{
qDebug() << "Can't Connect to DB !";
}
else
{
qDebug() << "Connected Successfully to DB !";
QSqlQuery query;
query.prepare("QUERY TO BE SENT TO THE DB");
if(!query.exec())
{
qDebug() << "Can't Execute Query !";
}
else
{
qDebug() << "Query Executed Successfully !";
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Qt - Gestire i database Sqlite
Nel file Project.pro aggiungiamo: CONFIG += sql
in MainWindow.h scriviamo:
#include <QMainWindow>
#include <QSql>
#include <QDebug>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
QSqlDatabase db;
};
Ora in MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QSQLITE" , "CONNECTION NAME");
db.setDatabaseName("C:\\sqlite_db_file.sqlite");
if(!db.open())
{
qDebug() << "Can't Connect to DB !";
}
else
{
qDebug() << "Connected Successfully to DB !";
QSqlQuery query;
query.prepare("SELECT name , phone , address FROM employees WHERE ID = 201");
if(!query.exec())
{
qDebug() << "Can't Execute Query !";
}
else
{
qDebug() << "Query Executed Successfully !";
while(query.next())
{
qDebug() << "Employee Name : " << query.value(0).toString();
qDebug() << "Employee Phone Number : " << query.value(1).toString();
qDebug() << "Employee Address : " << query.value(1).toString();
}
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Qt - Gestire i database ODBC
Nel file Project.pro aggiungiamo: CONFIG += sql
in MainWindow.h scriviamo:
#include <QMainWindow>
#include <QSql>
#include <QDebug>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
QSqlDatabase db;
};
Ora in MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QODBC" , "CONNECTION NAME");
db.setDatabaseName("DRIVER={SQL Server};SERVER=localhost;DATABASE=WorkDatabase"); // "WorkDatabase" is the name of the database we want
db.setUserName("sa"); // Set Login Username
db.setPassword(""); // Set Password if required
if(!db.open())
{
qDebug() << "Can't Connect to DB !";
}
else
{
qDebug() << "Connected Successfully to DB !";
QSqlQuery query;
query.prepare("SELECT name , phone , address FROM employees WHERE ID = 201");
if(!query.exec())
{
qDebug() << "Can't Execute Query !";
}
else
{
qDebug() << "Query Executed Successfully !";
while(query.next())
{
qDebug() << "Employee Name : " << query.value(0).toString();
qDebug() << "Employee Phone Number : " << query.value(1).toString();
qDebug() << "Employee Address : " << query.value(1).toString();
}
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Qt - Gestire i database Sqlite in memoria
Nel file Project.pro aggiungiamo: CONFIG += sql
in MainWindow.h scriviamo:
#include <QMainWindow>
#include <QSql>
#include <QDebug>
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
QSqlDatabase db;
};
Ora in MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QSQLITE" , "CONNECTION NAME");
db.setDatabaseName(":memory:");
if(!db.open())
{
qDebug() << "Can't create in-memory Database!";
}
else
{
qDebug() << "In-memory Successfully created!";
QSqlQuery query;
if (!query.exec("CREATE TABLE employees (ID INTEGER, name TEXT, phone TEXT, address TEXT)"))
{
qDebug() << "Can't create table!";
return;
}
if (!query.exec("INSERT INTO employees (ID, name, phone, address) VALUES (201, 'Bob', '5555-5555', 'Antarctica')"))
{
qDebug() << "Can't insert record!";
return;
}
qDebug() << "Database filling completed!";
if(!query.exec("SELECT name , phone , address FROM employees WHERE ID = 201"))
{
qDebug() << "Can't Execute Query !";
return;
}
qDebug() << "Query Executed Successfully !";
while(query.next())
{
qDebug() << "Employee Name : " << query.value(0).toString();
qDebug() << "Employee Phone Number : " << query.value(1).toString();
qDebug() << "Employee Address : " << query.value(1).toString();
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}
Rimuovere correttamente la connessione al database
Se vogliamo rimuovere alcune connessioni al database dall'elenco delle connessioni del database. abbiamo bisogno di usare QSqlDatabase::removeDatabase()
, tuttavia è una funzione statica e il modo in cui funziona è un po 'cablato.
// WRONG WAY
QSqlDatabase db = QSqlDatabase::database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
QSqlDatabase::removeDatabase("sales"); // will output a warning
// "db" is now a dangling invalid database connection,
// "query" contains an invalid result set
Di seguito è riportato il modo corretto che il documento Qt ci suggerisce.
{
QSqlDatabase db = QSqlDatabase::database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
}
// Both "db" and "query" are destroyed because they are out of scope
QSqlDatabase::removeDatabase("sales"); // correct
Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow