Registration Forgot password?
Login
Password
Remember me
Login through VKontakte
General > Articles > How to connect to Oracle with SYSDBA mode with Qt

How to connect to Oracle with SYSDBA mode with Qt

First of all, Qt 5 released before New Year so it's present for us :).

Unfortunately if you want using Qt as Oracle database client, and you need connect to DB as SYSDBA, you need provide small changes into Qt OCI driver.

Idea is simple - add connect option, that allow connect as SYSDBA. In file qsql_oci.cpp you can find 2 lines that look like OCISessionBegin (,,,, OCI_DEFAULT); last parameter related with connect mode (SYSDBA, SYSOPER, NORMAL и т.д.).

So we just add additional paramete into struct QOCIDriverPrivate. First of all add field in struct QOCIDriverPrivate, and do not forget initiate it, after that add code that will take input parameter and set needed connect mode in static void qParseOpts(const QString &options, QOCIDriverPrivate *d).


static void qParseOpts(const QString &options, QOCIDriverPrivate *d)

{

    const QStringList opts(options.split(QLatin1Char(';'), QString::SkipEmptyParts));

    for (int i = 0; i < opts.count(); ++i) {

        const QString tmp(opts.at(i));

        int idx;

        if ((idx = tmp.indexOf(QLatin1Char('='))) == -1) {

            qWarning("QOCIDriver::parseArgs: Invalid parameter: '%s'",

                     tmp.toLocal8Bit().constData());

            continue;

        }

        const QString opt = tmp.left(idx);

        const QString val = tmp.mid(idx + 1).simplified();

        bool ok;

        if (opt == QLatin1String("OCI_ATTR_PREFETCH_ROWS")) {

            d->prefetchRows = val.toInt(&ok);

            if (!ok)

                d->prefetchRows = -1;

        } else if (opt == QLatin1String("OCI_ATTR_PREFETCH_MEMORY")) {

            d->prefetchMem = val.toInt(&ok);

            if (!ok)

                d->prefetchMem = -1;

        // OUR NEW PARAMETER       

        } else if (opt == QLatin1String("OCI_ATTR_CONNECTION_MODE")) {

            if (val == QLatin1String("SYSDBA")) {

              d->ConnectionMode = OCI_SYSDBA;

            } else if (val == QLatin1String("SYSOPER")) {

              d->ConnectionMode = OCI_SYSOPER;

            } else {

              d->ConnectionMode = OCI_DEFAULT;

            }

        } else {

            qWarning ("QOCIDriver::parseArgs: Invalid parameter: '%s'",

                      opt.toLocal8Bit().constData());

        }

    }

}

Now we can use patched driver with new parameter OCI_ATTR_CONNECTION_MODE


void frmMain::openConnection() {

    OraConnect.setDatabaseName(CurrDatabase);

    switch(CurrMode) {

        case OCI_LOG_MODE_SYSDBA:

            OraConnect.setConnectOptions("OCI_ATTR_CONNECTION_MODE=SYSDBA");

            break;

        case OCI_LOG_MODE_SYSOPER:

            OraConnect.setConnectOptions("OCI_ATTR_CONNECTION_MODE=SYSOPER");

        break;

    }

    if (OraConnect.open(CurrLogin, CurrPass)) {

        LastError.clear();

        return true;

    } else {

        LastError = OraConnect.lastError().databaseText() + "\nDriver message: " +

                OraConnect.lastError().driverText();

        return false;

    }

}

All needed files you can download there qsql_oci.zip
You can add comment on current page, or on forum page there.Post count 2.

Last comment

01.10.2014
please check following line from example
OraConnect.setConnectOptions("OCI_ATTR_CONNECTION_MODE=SYSDBA");
also sent via email
24.09.2014
Hi, could you tell me how to use this new parameter in QSqlDatabase class? Thank you very much. Could you send me the method to my email?

Name*E-mail
Code*