0

QSqlError(-1, "Error opening database", "unable to open database file") when we try to open a qsqldatabase which having long database name(not supported more than 256 bytes)

I tried to remove directory path from database file name and change the current working directory by the above said directory, now file name is small but open call still failed

QString dbName = "/home/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789/abc123456789test.db"
QFileInfo fi(dbName);
QDir d = fi.dir();
char buffer[4096];
char* cwd = getcwd(buffer, 4096);
chdir(d.canonicalPath().toStdString().c_str());
m_sqldb = QSqlDatabase::addDatabase("QSQLITE",connectionName)
m_sqldb.setDatabaseName(fi.fileName());
if(!m_sqldb.isOpen()) {
    ok = m_sqldb.open();
    if (ok) {
    //////
    }
    else {
        qDebug()<<dbName<<m_sqldb.lastError();
        }
}
  • So it has nothing to do with a long database name? Please minimise your example further, see [mcve]. – m7913d Oct 01 '19 at 17:11
  • No, It is because of long file path only. In open call, internally it takes file name with absolute path. – kamlesh rathi Oct 02 '19 at 18:07
  • Does the full path to the file exist? I know QSqlDatabase::open() is supposed to create the _file_, but it doesn't seem to if all the folders are not there. – Ian May 19 '22 at 15:23

0 Answers0