qt连接Oracle数据库

2 篇文章 0 订阅

摘自官方文档 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();
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值