How to access to dbf file in QT 5.14 ?
-
Hi,
System: Windows 10
QT 5.14, Qt CreatorMy code:
//code start ======================================== QString dbFilePath = "D:\\data; //this is a directory //QString dbFilePath = "D:\\data\\tmp.dbf; //this is a dbf file,all tested QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); QString connString = QString("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;DBQ=%1").arg(dbFilePath); db.setDatabaseName(connString); bool ret = db.open(); QSqlQueryModel* p = new QSqlQueryModel; p->setQuery(QString("select col1,col2 from tmp"),db); //code end ===========================
running failed : QSqlQuery::exec: database not open
So how to access to dbf file in QT 5.14 ?
Regards
(mrJJ added code tags) -
Hi,
System: Windows 10
QT 5.14, Qt CreatorMy code:
//code start ======================================== QString dbFilePath = "D:\\data; //this is a directory //QString dbFilePath = "D:\\data\\tmp.dbf; //this is a dbf file,all tested QSqlDatabase db = QSqlDatabase::addDatabase("QODBC"); QString connString = QString("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;DBQ=%1").arg(dbFilePath); db.setDatabaseName(connString); bool ret = db.open(); QSqlQueryModel* p = new QSqlQueryModel; p->setQuery(QString("select col1,col2 from tmp"),db); //code end ===========================
running failed : QSqlQuery::exec: database not open
So how to access to dbf file in QT 5.14 ?
Regards
(mrJJ added code tags)@KellyWang First, add error handling to your code:
https://doc.qt.io/qt-5/qsqldatabase.html#lastError. -
Hi and welcome to devnet,
Beside the good point of @jsulm :
@KellyWang said in How to access to dbf file in QT 5.14 ?:
QString dbFilePath = "D:\data\tmp.dbf; //this is a dbf file,all
This is not a valid path, one backslash starts an escape sequence. If you want native Windows path you need to double all the backslashes or use the standard Unix notation and use QDir::toNativeSeparators.
-
Hi and welcome to devnet,
Beside the good point of @jsulm :
@KellyWang said in How to access to dbf file in QT 5.14 ?:
QString dbFilePath = "D:\data\tmp.dbf; //this is a dbf file,all
This is not a valid path, one backslash starts an escape sequence. If you want native Windows path you need to double all the backslashes or use the standard Unix notation and use QDir::toNativeSeparators.
Hi
@SGaist the poster has dual slashes but not using code tags so it's (was) not visible. (fixed)@KellyWang
Did you install the actual dbf drivers ?
They used to be part of
https://www.microsoft.com/en-us/download/details.aspx?id=13255
"Microsoft Access Database Engine 2010 Redistributable"
but I have no clue if the driver still is :)