Qt5_Oracle
1、编译驱动:
1.1、源码路径:F:\ZC_software_installDir\Qt5.3.2_vs2010\5.3\Src\qtbase\src\plugins\sqldrivers\ 里面有一些主流数据库的驱动源码,Oracle对应的是 文件夹"oci"
1.1.1、我将 文件夹"oci" 复制出来 ==> 不能这样编译,貌似 有需要别的东西
1.1.2、编译好的 .dll等文件,并不是 位于 路径"F:\ZC_software_installDir\Qt5.3.2_vs2010\5.3\Src\qtbase\src\plugins"下,而是 在"F:\plugins\sqldrivers"中...
1.2、编译时需要用到 Oracle的一些文件,需要在 oci.pro中添加如下内容:
INCLUDEPATH += F:\oracle\product\10.2.0\db_1\oci\include
LIBPATH += F:\oracle\product\10.2.0\db_1\oci\lib\msvc
2、测试代码:(QT 控制台程序)
2.1、?.pro
#-------------------------------------------------
#
# Project created by QtCreator 2016-07-07T13:22:41
#
#------------------------------------------------- QT += core \
sql #ZC: 要加上这个 QT -= gui TARGET = Qt5_DB_Test
CONFIG += console
CONFIG -= app_bundle TEMPLATE = app SOURCES += main.cpp LIBPATH += F:\plugins\sqldrivers #ZC: 编译时需要的lib文件的路径
2.2、qt.conf
[Paths]
plugins = ./plugins
ZC: 这里的意思是:插件的目录是 exe所在路径\plugin。于是Qt for Oracle 的驱动全文件名就是:exe所在路径\plugins\sqldrivers\qsqloci.dll
2.3、main.cpp
#include <QCoreApplication>
#include <QDebug> #include <QLibrary>
#include <QLibraryInfo> #include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlrecord>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDriver> #define DB_DRIVER "QOCI"
#define DB_HOSTNAME "192.168.1.201"
#define DB_DATABASENAME "ZHEJIANG" // ZC: 这里填的是 SID
#define DB_USERNAME "testWFpas"
#define DB_PASSWORD "dongruisoft.com" #pragma comment(lib, "qsqloci.lib") int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv); qDebug() << "QLibraryInfo::PrefixPath : " << QLibraryInfo::location(QLibraryInfo::PrefixPath);
qDebug() << "QLibraryInfo::DocumentationPath : " << QLibraryInfo::location(QLibraryInfo::DocumentationPath);
qDebug() << "QLibraryInfo::HeadersPath : " << QLibraryInfo::location(QLibraryInfo::HeadersPath);
qDebug() << "QLibraryInfo::LibrariesPath : " << QLibraryInfo::location(QLibraryInfo::LibrariesPath);
qDebug() << "QLibraryInfo::LibraryExecutablesPath : " << QLibraryInfo::location(QLibraryInfo::LibraryExecutablesPath);
qDebug() << "QLibraryInfo::BinariesPath : " << QLibraryInfo::location(QLibraryInfo::BinariesPath);
qDebug() << "QLibraryInfo::PluginsPath : " << QLibraryInfo::location(QLibraryInfo::PluginsPath);
qDebug() << "QLibraryInfo::ImportsPath : " << QLibraryInfo::location(QLibraryInfo::ImportsPath);
qDebug() << "QLibraryInfo::Qml2ImportsPath : " << QLibraryInfo::location(QLibraryInfo::Qml2ImportsPath);
qDebug() << "QLibraryInfo::ArchDataPath : " << QLibraryInfo::location(QLibraryInfo::ArchDataPath);
qDebug() << "QLibraryInfo::DataPath : " << QLibraryInfo::location(QLibraryInfo::DataPath);
qDebug() << "QLibraryInfo::TranslationsPath : " << QLibraryInfo::location(QLibraryInfo::TranslationsPath);
qDebug() << "QLibraryInfo::ExamplesPath : " << QLibraryInfo::location(QLibraryInfo::ExamplesPath);
qDebug() << "QLibraryInfo::TestsPath : " << QLibraryInfo::location(QLibraryInfo::TestsPath);
qDebug() << "QLibraryInfo::SettingsPath : " << QLibraryInfo::location(QLibraryInfo::SettingsPath);
qDebug() << ""; // ZC: 这个是,Oracle自带的驱动dll
//写清楚库的路径,如果放在当前工程的目录下,路径为./Oracle.so
QLibrary *libOCI = new QLibrary("F:\\oracle\\product\\10.2.0\\db_1\\bin\\oci.dll");
//加载动态库
libOCI->load();
if (!libOCI->isLoaded())
{
printf("Load Oracle oci.dll failed!\n");
return ;
} //QCoreApplication::addLibraryPath("F:\\plugins");
//*
// ZC: 这个是,Qt for Oracle的驱动dll
// ZC: 这里是手动加载 qsqloci.dll
// ZC: 将qsqloci.dll放在目录"F:\ZC_software_installDir\Qt5.3.2_vs2010\5.3\msvc2010_opengl\plugins\sqldrivers"中的话,会自动加载
// ZC: 貌似将qsqloci.dll放在 exe同目录,也不行...
QLibrary *libQSQLOCI = new QLibrary("F:\\plugins\\sqldrivers\\qsqloci.dll");
libQSQLOCI->load();
if (!libQSQLOCI->isLoaded())
{
printf("Load Qt sql driver for Oracle(qsqloci.dll) failed!\n");
return ;
}
//*/
QString strConnName = "testZC"; // ZC: 这个名字是随便填的,和Oracle的设置没有关系
QSqlDatabase sqldb; // 数据库指针
if (QSqlDatabase::contains(strConnName))
{
qDebug() << "QSqlDatabase::contains(" << strConnName << ")";
sqldb = QSqlDatabase::database(strConnName);
}
else
{
qDebug() << "! QSqlDatabase::contains(" << strConnName << ")";
sqldb = QSqlDatabase::addDatabase(DB_DRIVER, strConnName); // 使用Oracle数据库驱动
}
sqldb.setHostName(DB_HOSTNAME);
sqldb.setDatabaseName(DB_DATABASENAME);
sqldb.setUserName(DB_USERNAME);
sqldb.setPassword(DB_PASSWORD);
sqldb.setPort(); bool bRet = sqldb.open();
if (sqldb.isOpen())
bRet = true; if (! bRet)
{
qDebug() << "Oracle open failed : "+sqldb.lastError().text();
return ;
}
else
qDebug() << "Oracle open success ."; QSqlQuery query("select * from BUS", sqldb);
qDebug() << query.executedQuery();
qDebug() << "Record column count : " << query.record().count();
if(sqldb.driver()->hasFeature(QSqlDriver::QuerySize))
{
qDebug() << "QSqlDriver::QuerySize";
qDebug() << " Record row count (1) : " << query.size();
}
else
{
qDebug() << "! QSqlDriver::QuerySize";
query.last();
qDebug() << " Record row count (2) : " << (query.at() + );
// ZC: 上面已经指向最后一条记录了,要再遍历的话,要QSqlQuery.first()重新指向第1条记录了
qDebug() << " query.next() : " << query.next();
} qDebug() << "";
qDebug() << "isForwardOnly : " << query.isForwardOnly(); // ZC: 判断是 单向/双向?
query.last();
qDebug() << "Record row count (3) : " << query.numRowsAffected();
// ZC: numRowsAffected()返回的是 第1条记录 到 现在所指向的记录,一共是几条记录。,∴上面 先QSqlDriver.last(),然后才能得到记录总条数
// ZC: numRowsAffected() 貌似需要QSqlDriver是非单向(ForwardOnly)的 return a.exec();
}
3、路径:
F:\ZC_software_installDir\Qt5.3.2_vs2010\5.3\msvc2010_opengl\plugins\sqldrivers
./plugins/sqldrivers
4、Qt中 插件,要放在插件指定的路径中...
qsqloci.dll 必须放在 "??/plugins/sqldrivers"中
5、插件路径 操作
5.1、代码添加:
QCoreApplication::addLibraryPath("F:\\plugins");
5.2、 qt.conf 文件指定
6、注意点:
6.1、oci.dll 必须要加载
6.1.1、在系统Path 就是环境变量中添加 你的Oracle oci.dll库位置 C:\oracle\product\10.2.0\client_1\BIN。
6.1.2、代码中 使用QLibrary 加载oci.dll
6.2、qsqloci.dll 必须要加载
这里的具体做法 参考上面的"5"
7、
8、
Qt5_Oracle的更多相关文章
随机推荐
- git 下载单个文件 已经添加多个远程服务器
175down vote It is possible to do (in the deployed repository) git fetch followed by git checkout or ...
- 浅谈Android View滑动和弹性滑动
引言 View的滑动这一块在实际开发中是非常重要的,无论是优秀的用户体验还是自定义控件都是需要对这一块了解的,我们今天来谈一下View的滑动. View的滑动 View滑动功能主要可以使用3种方式来实 ...
- js模拟电梯操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- NC_Verilog中的工具ICC
Cadence中的Incisive Comprehensive Coverage(ICC) solusion提供在仿真中的覆盖率分析. ICC中的覆盖率类型有两大类: 1)Code Coverage: ...
- 10588 - Queuing at the doctors
这题wa 了 八次 你说 巨弱 orz 大神 总结一下 没有将所有的情况考虑清楚 ,当有的时候一个人已经全部看完的时候 别的人还没开始 但是我就把他给结束了 #include <iost ...
- jstat命令查看tomcat进程提示进程没找到(PID not found
今天遇到了一个小问题,我想用jstat命令查看tomcat进程(PID=24493)的内存使用情况,命令如下:jstat -gc 24493. 然后就报错了,错误提示信息为 24493 not fou ...
- Python: yield, python 实现tail -f
def CreateGenerator(file): with open(file,'r') as t: t.seek(0,2) while True: line=t.readline() if no ...
- python3.4学习笔记(二) 类型判断,异常处理,终止程序
python3.4学习笔记(二) 类型判断,异常处理,终止程序,实例代码: #idle中按F5可以运行代码 #引入外部模块 import xxx #random模块,randint(开始数,结束数) ...
- web前端----JavaScript的DOM(二)
前面在DOM一中我们知道了属性操作,下面我们来了解一下节点操作.很重要!! 一.节点操作 创建节点:var ele_a = document.createElement('a');添加节点:ele_p ...
- Quartz框架调用Demo
Quartz框架调用Demo 任务调度在JAVA应用程序中运用的十分普遍,掌握QUARTZ是必备的技能; 官网:http://www.quartz-scheduler.org/ 下载最新1.80资源包 ...