Qt5.5.1编译OCI驱动教程连接到Oracle数据库方法

http://www.mamicode.com/info-detail-1942114.html

一、qt安装需要安装完整版,包括src的

二、安装Oracle驱动win32_11gR2_client.zip

三、编译oci.pro工程,得到qsqlocid相关的dll,复制到X:\Qt\Qt5.5.1\5.5\mingw492_32\plugins\sqldrivers\里面

Qt5.5.1编译OCI驱动教程连接到Oracle数据库方法_第1张图片

四、验证是否支持

#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug()<<"Available drivers:";

    QStringList drivers=QSqlDatabase::drivers();

    foreach(QString driver,drivers)
        qDebug()<
#include 
#include 

#include 
#include 
#include 
#include 


#define SQLCMD "SELECT a.tablespace_name name, "\
"total/(1024*1024) total, "\
"free/(1024*1024) free, "\
"(total-free)/(1024*1024) used, "\
"round((total-free)/total,4)*100 used_percent "\
"FROM (SELECT tablespace_name,SUM(bytes) free "\
"FROM dba_free_space "\
"GROUP BY tablespace_name)a,  (SELECT tablespace_name,SUM(bytes) total "\
"FROM dba_data_files  GROUP BY tablespace_name)b  WHERE a.tablespace_name=b.tablespace_name"


int main(int argc, char *argv[])
{
    //查看所有数据库驱动
    QStringList lists = QSqlDatabase::drivers();

    for(int i = 0 ;i < lists.size(); ++ i)
    {
        qDebug() << lists.at(i);
    }
    //载入oracle驱动,打开本地数据库
    QSqlDatabase db2=QSqlDatabase::addDatabase("QOCI");
    db2.setHostName("127.0.0.1");
    db2.setPort(1521);
    db2.setUserName("root");
    db2.setPassword("123456");
    db2.setDatabaseName("orcl");
    if(!db2.open())
    {
        qDebug() <<"error_Oracle:\n" << db2.lastError().text();
        return 0;
    }
    else
    {
        qDebug() <<"success";
        QSqlQuery query;
        bool isok = query.exec(SQLCMD);
        if(!isok)
        {
            db2.close();
            return 0;
        }
        qDebug() <<"Name|Total|Free|Used|Used_percent";
        while (query.next()) {
            QString Str0 = query.value(0).toString();
            QString Str1 = query.value(1).toString();
            QString Str2 = query.value(2).toString();
            QString Str3 = query.value(3).toString();
            QString Str4 = query.value(4).toString();
            qDebug()<

 

你可能感兴趣的:(Oracle,QT)