mercredi 6 juillet 2016

How to check if file is appropriated with database driver in Qt


Assuming I want to open a connection to SQLite3 database in Qt. It will be connected to an existed database dbName. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); Now I need to set db.setDatabaseName(dbName), where dbName is received through a QFileDialog. However, as long as dbName is a valid file name, then db.open() is always true. Is there anyway to check if the file dbName is a SQLite3 database, but not of any other type? What I can do now is to execute a query and check for error like: db.setDatabaseName(dbName); if (db.open()) { QSqlQuery qr(db); if (!qr.exec(".database;")) { qDebug() << qr.lastError().text(); return false; } } but it won't work if the file is empty.

Aucun commentaire:

Enregistrer un commentaire