PDA

View Full Version : connection with qsql module



adamatic
24th February 2009, 08:49
Hello.

I am begginer with qsql module and I am trying to insert a new element in a database table. I dont know what i am doing wrong, because the conection is not opened. I am working with Windows 98.

This is the code:


QSqlDatabase db;
db = QSqlDatabase::addDatabase( "QODBC" );
db.setHostName("localhost");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=C:\\Documents and Settings\\user1\\bbdd.mdb");


if ( db.open() ) {
// Database successfully opened; we can now issue SQL commands.
QSqlQuery target;
target.exec( "INSERT INTO Equipo ( serialNumber,uniNumber,description ) VALUES ( 1213,3423,sdfahsoew )" );

}
db.close();

and it has never gone into the condition if.

Cheers, and thank you in advance.

spirit
24th February 2009, 09:22
try to connect to your database using QTDIR/demos/sqlbrowser if it will not connect then try to put your database in dir path to which doesn't have spaces.

adamatic
24th February 2009, 10:07
Ok, It is true that the puth it was too long, I put the database in the same directory of my project but the same problem continues, using the sqlbrowser tells:

the data source wasnt specify and it wasnt specified any predetermined controler.

have i to write only the driver and the database name bbdd.mdb?

spirit
24th February 2009, 10:12
database connection string looks fine except spaces in path.

adamatic
24th February 2009, 10:43
I can go into the if condtion now, but it isnt working the insertion in the table, is the insertion correct ?

talk2amulya
24th February 2009, 10:48
the third value in it seems to be a string, so try adding quotes to it :)

spirit
24th February 2009, 11:02
or use QSqlQuery::addBindValue.

adamatic
24th February 2009, 11:24
Before try with addBindValue tell you that I obtained the following warning and maybe it is the reason:

warning: Lowest section in C:\WINDOWS\system32\odbcint.dll is .rsrc at 1f841000

Because I could check that the connection with the database it is well done because when I have the database opens and program execution generates a .ldb. Although the access to the table is not working.

adamatic
24th February 2009, 11:38
Ok, I have just found the error, it was that i was writing double quotes( " ) and it must be simples ( ' ).

Sorry :o but thank for your time. Maybe this can advise someone about quotes.

Cheers