Qt QSql: cannot create a new MDB-File per ODBC
-
I am using the following code to create a NEW MS Access file using the SQL API from Qt.
QString strDatabaseName; if (strExt == ".DB") { oQtDatabase = QSqlDatabase::addDatabase(_TN("QSQLITE"), _TN("Redline_DB")); strDatabaseName = strDatei; } if (strExt == ".MDB" || strExt == ".ACCDB") { oQtDatabase = QSqlDatabase::addDatabase(_TN("QODBC"), _TN("Redline_MDB")); strDatabaseName = _TN("Driver={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DSN='';DBQ=") + QString::fromLatin1(strDatei); } oQtDatabase.setDatabaseName(strDatabaseName); if (!oQtDatabase.open()) throw oQtDatabase.lastError();The part for SQLite is succesful in creating a .DB File. Unfortunately QODBC throws an error:
[Microsoft][ODBC-Treiber für Microsoft Access] Datei '(unbekannt)' nicht gefunden. QODBC3: Unable to connectWhich means: ODBC-driver for Microsoft Access, File 'unkown' not found
I have to mention that the connection string works well with an existing Access file.
Thanks in advance for your help.
-
In general, you cannot connect to a database that does not already exist: as a file in the case of Access, or a service for MySql/Oracle etc. Sqlite is unusual in that it will create the file referenced if it does not exist (that can also fail).
You can programmatically create an Access database by automating Access (ActiveQt) and probably a bunch of other ways, including copying an "empty" template.