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的更多相关文章

随机推荐

  1. python中的re模块中的向后引用和零宽断言

    1.后向引用 pattern = re.compile(r"(\w+)")#['hello', 'go', 'go', 'hello'] # pattern = re.compil ...

  2. jmeter 正则表达式提取器的使用(提取第一个匹配结果)

    原文地址https://www.cnblogs.com/xueli/p/7405258.html?utm_source=itdadao&utm_medium=referral 正则表达式的用处 ...

  3. iOS 新浪微博-1.0框架搭建

    项目搭建 1.新建一个微博的项目,去掉屏幕旋转 2.设置屏幕方向-->只有竖向 3.使用代码构建UI,不使用storyboard 4.配置图标AppIcon和LaunchImage 将微博资料的 ...

  4. 移动端1px细线解决方案总结

    现在的PM和UI总以看app的眼光看html5, html页面要做的专业美观,而且必须很精细. 去年的时候UI就告诉我h5上的边框线太粗,把整站都给拉low了. 当时工期紧就没太在意1px粗细, 好在 ...

  5. php深入学习

    关于PHP程序员解决问题的能力 http://rango.swoole.com/archives/340 深入理解PHP内核 by xuhong大牛 http://www.php-internals. ...

  6. cordova+Android Studio 1.0+ionic+win7(转)

    转自http://blog.csdn.net/fuyunww/article/details/42216125 目录(?)[-] 在项目目录下执行 a创建工程 b添加平台支持 c添加插件在Androi ...

  7. bootstrap 带有确定取消按钮的modal

    </div><div class="modal fade" id="confirmModal" tabindex="-1" ...

  8. Mysql的group by语句

    如上图查询结果,因为group by后面的字段是的分组的依据字段,所以select子句中只有是group by后面的字段,或者是聚集函数才有意义.然而mysql居然可以在select子句中出现不在gr ...

  9. How To View the HTML Source in Google Chrome

    Whether you are new to the web industry or a seasoned veteran, viewing the HTML source of different ...

  10. 去n的第一个出现的1

    实例十八:去n的第一个出现的1 方法:result=n & (n-1) 与实例十七 思路类似.实例十七是不断取1,本例只去最低位. 解释:n 0000 1111n-1 0000 1110&am ...