(Necessitas)Microsoft sql db access gives error in opening

265 Views Asked by At

I wrote a program to access Microsoft sql database. It works fine in desktop. But when I run same program in android device I’m getting error:

04-09 06:17:41.784: W/Qt(1240): kernel\qsqlquery.cpp:368 (bool QSqlQuery::exec(const QString&)): QSqlQuery::exec: database not open

Here is my code:

 QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={SQL Server};Server=192.168.1.3;Database=sms_exp;");
db.setUserName("sa");
db.setPassword("xxxxxyy");
if(!db.open())
{
    qDebug ("Error");
}
else
{
    qDebug ("OK");
}

QSqlQuery query (db);
query.exec("SELECT item_code, item_des FROM prod_mast WHERE item_code = 0100000210");
while (query.next())
{
        QString name1 = query.value(0).toString();
        QString name2 = query.value(1).toString();
        qDebug (qPrintable(name1));
        qDebug (qPrintable(name2));
   }

db.close();
1

There are 1 best solutions below

7
On

There is no Android build of the ODBC driver. If you check QSqlDatabase::drivers() you will see that only SQLite driver is supported for android in Qt.

The only way of making an ODBC connection on android is to use a JDBC-ODBC bridge that supports network connections between the client and the server. JDBC on Android to ODBC on a windows machine and then to the MS Access ODBC driver.