OK guys, I figured out how to connect to my MS Access database using ODBC. You have to specify which driver your using for the ODBC connection type. If anyone cares here is the code to do that:
// Open connection.
mgDb.setHostName("localhost");
mgDb.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=MissileLogDB_USMTF.mda");
QSqlDatabase mgDb = QSqlDatabase::addDatabase("QODBC");
// Open connection.
mgDb.setHostName("localhost");
mgDb.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=MissileLogDB_USMTF.mda");
To copy to clipboard, switch view to plain text mode
I can now get a listing of the tables, but I am having problems doing basic SQL type queries (i.e. SELECT * FROM table). I get an error stating: QODBCResult::reset: Unable to allocate statement handle Error:
Here is the code:
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char ** argv)
{
// Open connection.
mgDb.setHostName("localhost");
mgDb.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=MissileLogDB_USMTF.mda");
if(mgDb.open() == false)
{
qDebug() << "ODBC::open() : error opening connection : "
<< mgDb.lastError().text() << "\n";
}
for(int i=0;i<list.size(); ++i)
{
qDebug() << "Table names " << list.at(i) << endl;
}
//This correctly returns the column indexes
int recNum = rec.indexOf("RecNum");
int uniqueId = rec.indexOf("UniqueID");
int sic = rec.indexOf("SIC");
int msgType = rec.indexOf("MsgType");
//This is giving me problems
if(!query.exec("SELECT * FROM EntityTypeTbl"))
qDebug() << query.lastError().text();
// Enter Qt main thread's event loop.
return app.exec();
} // main
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
QSqlDatabase mgDb = QSqlDatabase::addDatabase("QODBC");
QSqlQuery query;
// Open connection.
mgDb.setHostName("localhost");
mgDb.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=MissileLogDB_USMTF.mda");
if(mgDb.open() == false)
{
qDebug() << "ODBC::open() : error opening connection : "
<< mgDb.lastError().text() << "\n";
}
QStringList list = mgDb.tables(QSql::Tables);
for(int i=0;i<list.size(); ++i)
{
qDebug() << "Table names " << list.at(i) << endl;
}
QSqlDriver const * driver = query.driver();
//This correctly returns the column indexes
QSqlRecord rec = driver->record("EntityTypeTbl");
int recNum = rec.indexOf("RecNum");
int uniqueId = rec.indexOf("UniqueID");
int sic = rec.indexOf("SIC");
int msgType = rec.indexOf("MsgType");
//This is giving me problems
if(!query.exec("SELECT * FROM EntityTypeTbl"))
qDebug() << query.lastError().text();
// Enter Qt main thread's event loop.
return app.exec();
} // main
To copy to clipboard, switch view to plain text mode
Bookmarks