PDA

View Full Version : DB Connection Problem



ktmdwn
19th June 2008, 07:46
i had this Problem, DB Connection fail!

O.S :Windows_XP
tool :Qt_Program(Ver. 4.3.3) installed on VisualStudio 2005
DataBase : Oracle 10g

I want to Connect DataBase , this is my source code

frist, i added Qt Module(Sql)


#include <QtSql>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>

QSqlDatabase db;
db = QSqlDatabase::addDatabase("QOCI");
db.setHostName("192.168.1.223");
db.setDatabaseName("SSS");
db.setPort(1521);
db.setUserName("kkkk");
db.setPassword("1111");
bool ok = db.open();

and....... This defined tnsname.ora file

SSS =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.223)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = SSS)
)
)

compile Result : No error , but ok's value is false!

so... what's is the problem?

mcosta
19th June 2008, 09:13
A simple question:

Are you sure that OCI Driver is correctly loaded?

try with


if(!QSqlDatabaseisDriverAvailable("QOCI"))
{
qFatal("Driver not loaded");
}

Nokin
15th August 2008, 13:48
Ktmdwn, have you ever solve your problem? I have just like this problem.

Please, give your advise.:confused:

spirit
15th August 2008, 15:09
database name should looks like (in you case)

"(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.223)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = SSS)))"

Nokin
15th August 2008, 16:53
database name should looks like (in you case)

Spirit, as far as i can understand it's contest of tnsname.ora file?

Where i can find it? Thanks.

spirit
15th August 2008, 17:07
ORACLE_HOME\NETWORK\ADMIN\ -- under windows.
as I know under Linux the same path, but check :)

Nokin
18th August 2008, 16:04
ORACLE_HOME\NETWORK\ADMIN\ -- under windows.
as I know under Linux the same path, but check :)

Spirit, thanks.

Unfortunately, i cann't find this file =(. As far as i've understood this file is part of oracle server. But I have just Oracle Client (Oracle Client 10g Express Edition) and server is located in local netwoork.
What should i do for connect?:confused:

spirit
18th August 2008, 16:24
if you know ip-address of the server then you should ask the one who manage the server service name. :)

Ionwind
2nd August 2010, 12:22
Any progress on this one? After many tries I finally got the drivers working and now My code looks like this:



QSqlDatabase db = QSqlDatabase::addDatabase("QOCI");
db.setHostName("127.0.0.1");
db.setDatabaseName("dbname");
db.setPort(1521);
db.setUserName("user");
db.setPassword("pass");

if(!QSqlDatabase::isDriverAvailable("QOCI"))
{
// This happens if the driver is not available
qFatal("No driver!");
}
else if(!db.open())
{
// The drivers work but the connection cannot be opened, print out why.
QMessageBox::critical(0, QObject::tr("Database Error"), db.lastError().text());
}
else
{
// Everything works, maybe.
qDebug() << "Connection open!";
}
db.close();

And my tnsnames.ora looks like this:



MYDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = dbname)
)
)
I can connect to database with the Oracle client but I am unable to do so with Qt. The host ip is something else than 127.0.0.1 in this case.

With the code above I get (through line 14) ORA-12505 error (http://ora-12505.ora-code.com/):
"TNS:listener does not currently know of SID given in connect descriptor"

However, if I swap the parametres on lines 2 and 3 I get ORA-12545 (http://ora-12545.ora-code.com/):
"Connect failed because target host or object does not exist"

I've understood that the db.setHostName has nothing to do with the connection, it is just to identify the connection within Qt, am I right?

What am I doing wrong here? Please someone spell it out for me.

spirit
2nd August 2010, 12:26
try to pass this line




MYDB =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = dbname)
)
)


into QSqlDatabase::setDatabaseName.

Ionwind
2nd August 2010, 12:50
I tried that too, it gives me different error: ORA-12145 (http://ora-12145.ora-code.com/):
"TNS:could not resolve the connect identifier specified"
Any other ideas? : /

spirit
2nd August 2010, 12:53
where did you try to set up connection: on linux or windows? did you try to connect to a database using the SqlBrowser from Qt demos directory?

Ionwind
2nd August 2010, 14:23
I'm running on 32-bit Win XP, Qt Creator 2.0.0, on Qt 4.7.0. I've never tried out the SqlBrowser on the demos. Now when I tried it, I just got a message a through the application output after building: "The process could not be started!"

Ionwind
4th August 2010, 16:12
I solved my problem!
I connect to the database with the client with database name bar.foo and I tried to put that as parameter to QSqlDatabase::setDatabaseName() too. The connection, however, opened when I put there just foo (as in not bar.foo.

So the right values were following:


QSqlDatabase db = QSqlDatabase::addDatabase("QOCI");
db.setHostName("127.0.0.1"); // The TCP/IP of the host goes here
db.setDatabaseName("foo"); // As suggested, the database name goes here :P
db.setPort(1521);
db.setUserName("user");
db.setPassword("pass");