1 year ago
#343858

byjove
QtSQL error: SQLITE driver doesn't recognize my DB file as a database
the title mostly explains the problem.
I am using Qt 6 and the SQL module to connect to a external SQL database file (format is simply .db), in order to learn how SQL management via Qt works.
I learned a lot, but I am stuck with one last problem which prevents me to send requests and use my data : it seems like QtSQL is able to create a connection to my SQL database, but then, is FULLY UNABLE to recognize the file as a database, and makes my query unactive.
One error log from the terminal will be more useful than a whole post trying to find words so I could explain correctly the thing:
=> Successfully connected to DB // db.open()
ERROR: file is not a database //... isActive()
Can't execute the instruction
The code:
QSqlDatabase sqlDb = QSqlDatabase::addDatabase("QSQLITE");
sqlDb.setDatabaseName(m_pathToDb + "db.sql");
if (sqlDb.open()){
std::cout << "Successfully connected to DB" << std::endl;
} else {
std::cerr << "Can't connect to SQL - " << sqlDb.lastError().text().toStdString() << std::endl;
}
QSqlQuery query = sqlDb.exec("SELECT * FROM table_1");
if(!query.isActive()){
std::cout << "ERROR: " << query.lastError().text().toStdString() << std::endl;
} else {
while (query.next()){
std::cout << query.value(1).toString().toStdString() << std::endl;
}
}
I merely translated the very last line from French, so expect it to be not exactly what SQLite would've returned in English.
Oh, and yes, the m_pathToDb variable has a correct value, I checked it several times. I don't know how SQLite internally works so I am lost for now. Thanks in advance for anyone who'll help me.
c++
sql
sqlite
qt
qtsql
0 Answers
Your Answer