FireBird简介
Firebird是典型的嵌入式数据库。所谓嵌入式数据库,简单地说就是不需要单独数据库服务器安装配置,只需要在应用软件中加入客户端接口就可以直接把一个文件当做数据库存取。
编译方法1
1、从官网(https://firebirdsql.org/en/server-packages/)或(https://sourceforge.net/projects/firebird/files/)下载相同版本的普通版及embed版本的FireBird数据库压缩包。此处以Firebird-2.5.9.27139-0_x64_pdb&Firebird-2.5.9.27139-0_x64_embed_pdb为例。
2、在qt的安装目录找到ibase.pro文件,修改ibase.pro为下例所示:
TARGET = qsqlibase
HEADERS += D:/Qt/5.15.2/Src/qtbase/src/plugins/sqldrivers/ibase/qsql_ibase_p.h
SOURCES += D:/Qt/5.15.2/Src/qtbase/src/plugins/sqldrivers/ibase/qsql_ibase.cpp D:/Qt/5.15.2/Src/qtbase/src/plugins/sqldrivers/ibase/main.cpp
INCLUDEPATH += "D:/Firebird-2.5.9.27139-0_x64_pdb/include"
LIBS += "D:/Firebird-2.5.9.27139-0_x64_pdb/lib/fbclient_ms.lib"
# FIXME: ignores libfb (unix)/fbclient (win32) - but that's for the test anyway
#QMAKE_USE += ibase
OTHER_FILES += ibase.json
PLUGIN_CLASS_NAME = QIBaseDriverPlugin
include(../qsqldriverbase.pri)
3、修改qsqldriverbase.pri中的内容如下所示:
QT = core core-private sql-private
# For QMAKE_USE in the parent projects.
include(./configure.pri)
PLUGIN_TYPE = sqldrivers
load(qt_plugin)
DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII
4、修改完使用qt-creator直接进行编译,则会在D:\plugins\sqldrivers中生成相关的数据库dll,拷贝qsqlibase.dll和qsqlibased.dll两个数据库至数据库安装目录D:\Qt\5.15.2\msvc2019_64\plugins\sqldrivers。
5、拷贝embed版中的fbembed.dll、icuin30.dll、icudt30.dll、icuuc30.dll至执行程序目录,并将fbembed.dll更名为fbclient.dll。
测试连接代码如下:
void Dialog::on_pushButton_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE");
db.setUserName("SYSDBA");
db.setPassword("masterkey");
db.setDatabaseName("D:/prj.fdb");
if (db.open())
{
QString strSql = QString("select user_name, user_pwd from project_users where level_num = 0;");
QSqlQuery query;
query.exec(strSql);
while (query.next()) {
QString name = query.value("user_name").toString().trimmed();
QString pwd = query.value("user_pwd").toString().trimmed();
qDebug() << name << pwd;
}
db.close();
} else {
qDebug() << "Failed to connect to Firebird database:" << db.lastError().text();
}
}
编译方法2
1、查看QT帮助文档,可知道数据库驱动编译相关介绍
How to Build the QIBASE Plugin on Unix and macOS
The following assumes InterBase or Firebird is installed in /opt/interbase:
If you are using InterBase:
cd $QTDIR/qtbase/src/plugins/sqldrivers
qmake -- IBASE_PREFIX=/opt/interbase
make sub-ibase
If you are using Firebird, the Firebird library has to be set explicitly:
cd $QTDIR/qtbase/src/plugins/sqldrivers
qmake -- IBASE_PREFIX=/opt/interbase IBASE_LIBS=-lfbclient
make sub-ibase
How to Build the QIBASE Plugin on Windows
The following assumes InterBase or Firebird is installed in C:\interbase:
If you are using InterBase:
cd %QTDIR%\qtbase\src\plugins\sqldrivers
qmake -- IBASE_INCDIR=C:/interbase/include
nmake sub-ibase
nmake install
If you are using Firebird, the Firebird library has to be set explicitly:
cd %QTDIR%\qtbase\src\plugins\sqldrivers
qmake -- IBASE_INCDIR=C:/interbase/include IBASE_LIBS=-lfbclient
nmake sub-ibase
nmake install
If you are not using a Microsoft compiler, replace nmake with mingw32-make in the line above.
Note that C:\interbase\bin must be in the PATH.
2、根据最后一段描述,首先打开命令窗口
cd %QTDIR%\qtbase\src\plugins\sqldrivers
qmake INCLUDEPATH+="D:/Firebird-2.5.9.27139-0_x64_pdb/include" LIBS+="D:/DCS_contrib/Firebird-2.5.9.27139-0_x64_pdb/lib/fbclient_ms.lib" ibase.pro
即可在数据库驱动目录生成相应的ibase数据库驱动动态库。