PDA

View Full Version : Qt Microsoft Access Connecion Problem on Windows



Furkan
27th September 2010, 12:34
Hello.
I've been working on a project I used QSqlite on Linux and it worked perfectly. But when I
decided to build my project on Windows QSqlite didn't work. So I decided
to connect to Access I used the connection string on Qt docs "DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb"
but when I run my project Qt gives me this error "QSqlQuery::exec: database not open
"QODBCResult::exec: No statement handle available" Error: "[Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'." "
Can you please help me!? It's so important!
Thanks in advance!
Note: I tried compiling ODBC driver.

marcvanriet
27th September 2010, 12:50
Hi,
This works for me :


*dbDci = QSqlDatabase::addDatabase("QODBC");
dbDci->setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MSAccess};DBQ=dci.mdb");
dbDci->open();


'dci.mdb' is the name of my MS Access file.

Also, in the .pro file of your project, you have to put QT += sql.
Are the odbc DLL's present in your plugins directory ?

Best regards,
Marc

tbscope
27th September 2010, 12:54
But when I
decided to build my project on Windows QSqlite didn't work.

If you're still interested in using SQLite on Windows too, can you give us the error you got?

Furkan
27th September 2010, 17:37
It doesn't give any errors but when I debug my code, at while(readPassword.next()) step nothing happens. I'm pretty sure that the database contains passwords and usernames. There is nothing wrong with the code because it works on Linux. And in this directory "C:\Qt\2010.05\qt\plugins\sqldrivers" qsqlite4.dll, qsqlited4.dll, qsqlodbc4.dll, qsqlodbcd4.dll files exist.

Furkan
27th September 2010, 18:05
OK guys! I've solved the problem. After installing the drivers it worked. I thought I installed them but apparently I didn't. Thank you all for your support.

Furkan
28th September 2010, 17:06
There is a problem again. It worked once and startted giving errors. Here is the code. Please help me I've been trying to solve it ever since it gave me the problem. Please guys!

QSqlDatabase database = QSqlDatabase::addDatabase("QODBC");
database.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MSAccess};DBQ=Database.mdb");
database.open();
QMessageBox::information(this, "1212321123123213123123", database.lastError().text());
QSqlQuery readPassword("SELECT * FROM password WHERE username=:username");
readPassword.bindValue(":username", ui->lineEdit_2->text());
readPassword.exec();
if(ui->lineEdit->text().isEmpty() || ui->lineEdit_2->text().isEmpty())
{
QMessageBox::information(this,"Hata", QString::fromUtf8("Fill in the blanks!"));
database.close();
this->show();
}
else
{
bool yes = false;
while(readPassword.next())
{
QString string = readPassword.value(0).toString();
QString string1 = readPassword.value(1).toString();
if(string == ui->lineEdit->text() && string1 == ui->lineEdit_2->text())
{
database.close();
notTakip.show();
notTakip.setPermitedUsername(ui->lineEdit_2->text(), readPassword.value(2).toString().toInt());
notTakip.prepareNotTakip();
yes = true;
this->close();
break;
}
}
if(yes == false)
{
QMessageBox::information(this, "Error", readPassword.lastError().text());
QMessageBox::information(this,"Error", QString::fromUtf8("Wrong username or password!"));
ui->lineEdit->clear();
database.close();
this->show();
}
}

The error when I run it with Qt Creator: {Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'. QODBC3: Unable to connect.
When I run the executable the error is: {Microsoft][ODBC Driver Manager] Function sequence error QODBC3: Unable to execute statement.

Furkan
28th September 2010, 17:36
Also QSqlDatabase::isDriverAvailable("QODBC") returns true!

saa7_go
28th September 2010, 18:54
The error when I run it with Qt Creator: {Microsoft][ODBC Microsoft Access Driver] Could not find file '(unknown)'. QODBC3: Unable to connect.

Make sure that your database file is in the same directory with your executable.



When I run the executable the error is: {Microsoft][ODBC Driver Manager] Function sequence error QODBC3: Unable to execute statement.

Modify your codes from


QSqlQuery readPassword("SELECT * FROM password WHERE username=:username");
readPassword.bindValue(":username", ui->lineEdit_2->text());
readPassword.exec();


to


QSqlQuery readPassword;
readPassword.prepare("SELECT * FROM password WHERE username=:username");
readPassword.bindValue(":username", ui->lineEdit_2->text());
readPassword.exec();

Furkan
29th September 2010, 12:10
Thank you so much! It worked. But Can you tell me the reason why use prepare?
And when I try to run the executable it works nice but on another computer with necessary Qt dlls (Qt Framwork isn't installed) it doesn't work. Gives this error "Database driver not loaded".
Do you know how to fix it?

Furkan
29th September 2010, 12:18
Thank you all. I've solved the problem.
From this link (http://www.qtcentre.org/threads/12498-SQlite-driver-not-loaded-error)

saa7_go
30th September 2010, 07:16
Can you tell me the reason why use prepare?

Because you use prepared statement. Take a look at QSqlQuery::prepare(const QString &) (http://doc.trolltech.com/4.6/qsqlquery.html#prepare).