4

I'm working on qt, my database was rightly connected with qt but suddenly i have the following problem every time i debug,,,i i become not able to fetch or to add data from/to the database,,, i don't know whats's the matter but i'm new to qt.

QSqlQuery::exec: database not open

could anybody help please ,,it's an emergency case here the code

db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DSN='';DBQ=C:/Users/user/Desktop/Final_Version/db.accdb");
db.close();
db.open();
QSqlQuery query;
query.exec("Select ID from TestId");
while(query.next())
{
 TestId = query.value(0).toInt();
}
db.close();
//==================================================================================
RAM
  • 2,257
  • 2
  • 19
  • 41
Nermeeno Alami
  • 69
  • 1
  • 4
  • 9

3 Answers3

8
QSqlQuery *query = new QSqlQuery(db);

I think that can help you! :)

Linga
  • 10,379
  • 10
  • 52
  • 104
3

You need to add qsql dll files for Microsoft Access Driver within your sqldrivers folder

Check following if you are debugging

  1. check your project settings for dll reference(in .pro file of your project, you have to put QT += sql, if you are using Visual studio integration you check reference in project settings form properties)
  2. check you Qtdirectory for driver folder and dll's

If you are distributing your app, you just copy sql driver folder also with your executable along with required dlls.

edit your QSqlQuery object binded with database connection

db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb};FIL={MSAccess};DBQ=C:/Users/user/Desktop/Final_Version/db.mdb");
database.open();
QSqlQuery query(db);
query.exec("Select ID from TestId");
while(query.next())
{
    TestId = query.value(0).toInt();
}
db.close();

I think, some of this will help you... :)

RAM
  • 2,257
  • 2
  • 19
  • 41
imalvare
  • 295
  • 1
  • 13
  • Thank you very much i will try now but please stay around i may need you ... i was waiting for answer since yesterday – Nermeeno Alami Jun 01 '13 at 11:26
  • could you please tell me did you mean by "check you Qtdirectory for driver folder and dll's" and how can i check it – Nermeeno Alami Jun 01 '13 at 11:56
  • on your installation path there will be a folder named "sqldrivers" with some dlls for database connection. try to locate it. eg: qsqlite4.dll, qsqlite4d.dll etc. If there is missing dlls it may not working. – imalvare Jun 01 '13 at 12:18
1

try not to use db.open() check the database opened or not. It's not that accurate, check this out, after the check you might be able use db.open() and exec(command) normally(link with query first).

QSqlQueryModel *model = new QSqlQueryModel;
if (db.isValid())
{
    db.open();
    if (db.isOpen())
    {
        QSqlQuery query(db);
        query.prepare("SELECT * FROM version_chart");
        query.exec();
        if (query.isActive())
        {
            model->setQuery(query);
            db.close();
            qDebug() << "opened success\n";
        }
        else {
            qDebug() << "query is not active:DB being occupied, close manually";
            qDebug() << db.lastError().text();
            qFatal("failed to connect.");
            return false;
        }
    }
    else {
        qDebug() << "DB is not open";
        return false;
    }
}
else {
    qDebug() << "DB is not valid";
    return false;
}

Here's also another way I'm using right now. I may simply change the file name and check if it succeed.

if (isFileUsed(origin_path))
    {
        qDebug() << "Warning: File being occupied!" << "\n";
        QMessageBox::critical(0,"File Open Error", "File occupied, please close and retry!",
            QMessageBox::Ok, 0);
        return false;
    }

bool Save_DB::isFileUsed(QString fpath)
{
    bool isUsed = false;

    QString fpathx = fpath + "x";

    QFile file(fpath);
    bool isExist = file.exists();
    if (isExist == true)
    {
        bool isCanRename = file.rename(fpath, fpathx);
        if (isCanRename == false)
        {
            isUsed = true;
        }
        else
        {
            file.rename(fpathx, fpath);
        }
    }
    file.close();

    return isUsed;
}