0

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();
A.J
  • 725
  • 10
  • 33

1 Answers1

0

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.

Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Is it possible to include JDBC-ODBC bridge in my application? – A.J Apr 09 '14 at 10:43
  • May be it is possible using JNI. http://en.wikipedia.org/wiki/Java_Native_Interface – Nejat Apr 09 '14 at 11:00
  • Is it possible for connecting to Microsoft database with SQLite driver in android device? – A.J Apr 09 '14 at 11:25
  • No SQLite driver is for SQLite databases. MS Access and MS SQL Server need ODBC driver which is not supported on Android. If i were you i would use a SQLite database. – Nejat Apr 09 '14 at 11:29
  • I am developing a subsidiary application, where the main application is for desktop. And it uses Microsoft db – A.J Apr 09 '14 at 11:36
  • Any idea how to install JDBC-ODBC bridge on android device manually? – A.J Apr 09 '14 at 11:37
  • I have no idea of how to make it work. But on android i think it is simpler to convert your db to SQLite and use it. – Nejat Apr 09 '14 at 11:55