PDA

View Full Version : SQL connection problem



Mrdata
24th May 2007, 11:15
I've the following problem while trying to access a sql database through ip address
My code:




QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("66.xx.xxx.xxx");//ip masked for privacy
db.setDatabaseName("john_stuff");
db.setUserName("john_stuff");
db.setPassword("12345");


bool ok = db.open();
error = db.lastError();
if( ok )
{
...
}


I tried using different sql driver, QSQLITE, QPSQL and QPSQL7

Results using:
QSQLITE: ok = true, no errors throwed but no matter what ip address, username, password i enter it always say it's ok?
No firewall warnings.

QPSQL and QPSQL7: ok = false, lasterror always says
driver error: QPSQL: Unable to connect
databaseError: {"could not connect to server: Can't assign requested address (0x00002741/10049) Is the server running on host "66.xx.xxx.xxx" and accepting
TCP/IP connections on port 5432? " size=162}
ErrorType: ConnectionError
errorNumber: -1
My firewall throws a warning so it's really trying to connect to something online

I didn't understood the port warning so i tried using db.setPort( 3306 ) because in the past i used a program that used this port number to access the same database and it worked ok.
Results were: ok = false
databaseError {"expected authentication request from server, but received A" size=60}


I know that my available drivers are: QSQLITE QODBC3 QODBC QPSQL7 QPSQL

I'm total newbie at sql stuff, what i'm doing wrong here?

jacek
24th May 2007, 19:01
What database you want to connect to? Judging from the port, it's MySQL, so most likely you have to use QMYSQL driver.

mm78
24th May 2007, 20:14
QSQLITE always works because it doesn't connect to a remote database, but creates a local one on your computer. See http://www.sqlite.org/ for more info on SQLite.

As Jacek says you need to know what kind of database you're trying to connect to. Is it PostgreSql or MySql or something else? And you need to set the correct port as well, unless the database uses its default port.

If it's a MySql database as the port number suggests (3306) you need to build the QMYSQL driver and use this.