qt编译oracle驱动,qt 5.12 连接 oracle 数据库示例代码
-
下载安装oracle客户端 oracle11g R2 64位
-
安装qt 5.12
-
D:\alantop_sde\Qt\5.12.5\Src\qtbase\src\plugins\sqldrivers\oci
打开 oci.pro
TARGET = qsqloci
HEADERS += $$PWD/qsql_oci_p.h
SOURCES += $$PWD/qsql_oci.cpp $$PWD/main.cpp
#QMAKE_USE += oci
darwin:QMAKE_LFLAGS += -Wl,-flat_namespace,-U,_environ
OTHER_FILES += oci.json
PLUGIN_CLASS_NAME = QOCIDriverPlugin
include(../qsqldriverbase.pri)
QMAKE_LFLAGS += D:/oracle11g/product/11.2.0/client_1\BIN/oci.dll
INCLUDEPATH += D:/oracle11g/product/11.2.0/client_1/oci/include
LIBPATH += D:/oracle11g/product/11.2.0/client_1/oci/lib/msvc
#QMAKE_USE += oci
QMAKE_LFLAGS += D:/oracle11g/product/11.2.0/client_1\BIN/oci.dll
INCLUDEPATH += D:/oracle11g/product/11.2.0/client_1/oci/include
LIBPATH += D:/oracle11g/product/11.2.0/client_1/oci/lib/msvc
操作系统,数据库,编译器都选择64为的 使用MinGw64
编译出现错误,修改源码
修改2个地方 修改源码 qsql_oci.cpp 1599行
1. 修改函数名称 OCIBindByPos2改为OCIBindByPos
2. 把bindColum.lengths 变量做指针转换 reinterpret_cast<ub2*>(bindColumn.lengths),
修改后编译后,在 D:\plugins\sqldrivers 目录下生成驱动
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QtGlobal>
#include <QtDebug>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>
int main()
{
qDebug()<<"Available drivers:";
QStringList drivers=QSqlDatabase::drivers();
foreach(QString driver,drivers)
qDebug()<<driver;
QSqlDatabase db = QSqlDatabase::addDatabase("QOCI");
qDebug() << "QT DB driver?" << db.isValid();
bool m_bIsConn;
db.setHostName("10.2.3.102");
db.setPort(1521);
db.setDatabaseName("");
db.setUserName("");
db.setPassword("");
if(db.open())
{
qDebug()<<QString("连接成功");
m_bIsConn = true;
}
else
{
qDebug()<<"连接失败";
m_bIsConn = false;
qDebug()<<db.lastError();
}
QSqlQuery query("SELECT * FROM tables");
while (query.next())
{
int iProNo = query.value(0).toInt();
QString sProName = query.value(1).toString();
qDebug()<< sProName;
}
if(m_bIsConn)
{
QSqlQuery query;
bool bSuccess = query.exec("select * from DT_BASE_DTLS");
if(bSuccess)
{
QSqlRecord rec = query.record();
qDebug()<<("TableName的字段个数:")<<rec.count();
QSqlField filed = rec.field(0);
qDebug()<<("字段名称:")<<rec.field(0).name()<<("字段类型:")<<rec.field(0).type()<<("字段值:")<<rec.field(0).value();
qDebug()<<("段名称:")<<rec.field(1).name()<<("字段类型:")<<rec.field(1).type()<<("字段值:")<<rec.field(1).value();
qDebug()<<("字段名称:")<<rec.field(2).name()<<("字段类型:")<<rec.field(2).type()<<("字段值:")<<rec.field(2).value();
qDebug()<<("字段名称:")<<rec.field(3).name()<<("字段类型:")<<rec.field(3).type()<<("字段值:")<<rec.field(3).value();
}
else
{
//qDebug()<<tr("error");
}
}
}