Qt
Qt - Omgaan met databases
Zoeken…
Opmerkingen
- U hebt de Qt SQL-plug-in nodig die overeenkomt met het type dat wordt gegeven aan
QSqlDatabase::addDatabase
- Als u niet over de vereiste SQL-plug-in beschikt, waarschuwt Qt u dat het het gevraagde stuurprogramma niet kan vinden
- Als u niet over de vereiste SQL-plug-in beschikt, moet u deze compileren vanuit de Qt-bron
Een database gebruiken op Qt
In het Project.pro-bestand voegen we toe:
CONFIG += sql
in MainWindow.h schrijven we:
#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;
};
Nu 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 - Omgaan met SQL-databases
In het Project.pro-bestand voegen we toe: CONFIG += sql
in MainWindow.h schrijven we:
#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;
};
Nu 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 - Omgaan met ODBC-databases
In het Project.pro-bestand voegen we toe: CONFIG += sql
in MainWindow.h schrijven we:
#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;
};
Nu 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 - Omgaan met Sqlite-databases in het geheugen
In het Project.pro-bestand voegen we toe: CONFIG += sql
in MainWindow.h schrijven we:
#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;
};
Nu 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;
}
Verwijder de databaseverbinding correct
Als we een database-verbinding willen verwijderen uit de lijst met database-verbindingen. we moeten QSqlDatabase::removeDatabase()
, maar het is een statische functie en de manier waarop het werkt is een beetje bedraad.
// 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
De juiste manier die Qt Document ons suggereert, is hieronder.
{
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
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow