摘自官方文档 https://doc.qt.io/qt-5/sql-driver.html
- 要连接的前提首先是在qt安装过程中下载src,才有%QTDIR%\qtbase\src\plugins\sqldrivers
How to Build the OCI Plugin on Windows
Choosing the option “Programmer” in the Oracle Client Installer from the Oracle Client Installation CD is generally sufficient to build the plugin. For some versions of Oracle Client, you may also need to select the “Call Interface (OCI)” option if it is available.
- Build the plugin as follows (here it is assumed that Oracle Client is installed in C:\oracle):
cd %QTDIR%\qtbase\src\plugins\sqldrivers
qmake -- OCI_INCDIR=c:/oracle/oci/include OCI_LIBDIR=c:/oracle/oci/lib/msvc
nmake sub-oci
If you are not using a Microsoft compiler, replace nmake with mingw32-make in the line above.
When you run your application, you will also need to add the oci.dll path to your PATH environment variable:
运行app前,把oci.dll的目录加入到环境变量中
set PATH=%PATH%;c:\oracle\bin
下面是连接测试
#include "mainwindow.h"
#include <QApplication>
#include<QDebug>
#include<QSqlDatabase>
int main(int argc, char *argv[])
{
//初始化窗口系统
QApplication a(argc, argv);
//读取数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QOCI");
db.setHostName("localhost");
db.setDatabaseName("ORCL");
db.setPort(1521);
db.setUserName("system");
db.setPassword("orcl");
if(db.open())
{
qDebug()<<"open";
}else
{
qDebug()<<"open error";
}
MainWindow w;
w.show();
return a.exec();
}