PDA

View Full Version : How connect to Oracle DB



fengtian.we
9th April 2007, 15:32
How connect to Oracle DB

do you have some code show me?

jacek
9th April 2007, 17:46
Check out QSqlDatabase.

Note that Oracle driver is available for commercial Qt.

GreyGeek
9th April 2007, 19:18
Depends on your DBHOST name and DBNAME.
Here is part of main.cpp:


#ifdef _WIN32 // on Windows
#define DBDRIVER "QOCI"
#define DBHOST "orc1"
#define DBNAME "orc1"
#else // must be on Linux and PostgreSQL 8
#define DBDRIVER "QPSQL"
#define DBHOST "localhost"
#define DBNAME "homestead"
#endif

int main( int argc, char * argv[] ) {
...
QString strRejected = "";
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(false);
dlgLogin dlg; // gets name and password from login dialog
if( dlg.exec() == QDialog::Accepted ){
QSqlDatabase hapdb = QSqlDatabase::addDatabase(DBDRIVER);
hapdb.setHostName(DBHOST);
hapdb.setDatabaseName(DBNAME);
hapdb.setUserName(dlg.dui.leUserName->text());
hapdb.setPassword(dlg.dui.leUserPassword->text());
if ( hapdb.open() ) {
homestead ht;
ht.RevID = dlg.dui.leUserName->text();
ht.show();
app.setQuitOnLastWindowClosed(true);
return app.exec();
} else {
strRejected = QString("Reason: %1").arg(hapdb.lastError().text()).toLatin1();
QMessageBox::information(0,"Login Rejected!",strRejected,
QMessageBox::Ok,QMessageBox::NoButton,QMessageBox: :NoButton);
return 1;
}
} else {
strRejected = QString("User Canceled the login!").toLatin1();
QMessageBox::information(0,"Login Canceled!",strRejected,
QMessageBox::Ok,QMessageBox::NoButton,QMessageBox: :NoButton);
return 2;
}
}

Now, because hapdb is the default database in the application, any queries, etc. are against that database, so it does not have to be referenced. For more than one db open at the same time consult the Qt Assistant.

In the app init code:


// create query string that selects for proprty_id in property table
this->propPIDqryStr = "SELECT proprty_id, county, cntyname, txdistrict, legal, ";
this->propPIDqryStr.append("parcel_id, pvalue, pctext, totincome, entry_id, entry_date, ");
this->propPIDqryStr.append("appstatus, offuse, itotinc, ivalue, ipctex, notes FROM ");
this->propPIDqryStr.append(this->propertydb);
this->propPIDqryStr.append(" WHERE proprty_id = ");

In a search function:


if (ui.rbProprtyID->isChecked()) {
// user is searching for property id
requestString.append(" in Property");
ui.leStatus->setText(requestString);
propStr = this->propPIDqryStr;
propStr.append(ui.leSearch->text());
propQry->exec(propStr);
if (propQry->first()){
// found property record, now fetch associated persinfo record
foundProp = true;
persStr = this->persPIDqryStr;
persStr.append(ui.leSearch->text());
persQry->exec(persStr);
if (persQry->first()) {
foundPers = true;
}
}
} // end proprty_id

Other code retrieves the data from propQry and persQry and displays it.

fengtian.we
11th April 2007, 12:20
Thanks every one ~ I think this some good tip~ :) go on