Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Problem with SQLite database -> unable to open database file
Forum Updated to NodeBB v4.3 + New Features

Problem with SQLite database -> unable to open database file

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 3 Posters 9.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gandiii
    wrote on 4 Jun 2012, 10:03 last edited by
    #1

    Hello,

    my sqlite database worked fine for a while
    and now i cant write or read something.

    init of the database:
    @ _data= QSqlDatabase::addDatabase("QSQLITE","data");
    _data.setDatabaseName(DAODatabaseFilename);

    //Erzeugen der Tabellen in Datenbank
    QFileInfo info ("/home/data.db");
    if(!info.exists()){
    if(_DAODatabase.open()) {
    QSqlQuery* cmd = new QSqlQuery(_data);
    cmd->exec(QString("PRAGMA journal_mode = OFF"));

    cmd->exec(QString(DataTableCreation).arg(TableName));

    delete cmd;
    _data.close();
    }
    }
    @

    And this is how I use the database:
    @
    bool DB::setWertDataObject(DataObject* data)
    {
    if(!data){
    qCritical() << "Null-Pointer bei Wert-Set";
    return false;
    }
    if(_data.open()) {
    QSqlQuery* cmd = new QSqlQuery (_data);
    cmd->exec(QString(SelectData).arg(TableName).arg(data->ID));

    bool bResult;
    //Kein Eintrag in Datenbank gefunden
    if(!cmd->first()){
    //Insert
    bResult = cmd->exec(QString(InsertData).arg(data->ID).arg(data->fValue));
    }
    else{
    //Update
    bResult = cmd->exec(QString(UpdateData).arg(data->fValue).arg(data->ID));
    }
    if(bResult == false){
    qCritical()
    << "Database Error : ID "
    << data->ID
    << ", Error: "
    << cmd->lastError().databaseText();

    }
    delete cmd;
    _data.close();
    return bResult;
    }
    else{
    qWarning()
    <<"Cant open database";

    return false;
    }
    }@

    I always get the error: unable to open database file!

    I have tried to delete the file, change permissions, etc.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      absfrm
      wrote on 5 Jun 2012, 08:01 last edited by
      #2

      this code is working right
      @
      QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
      mydb.setDatabaseName(/your sqlite database file/);
      if (mydb.open())
      {
      QMessageBox::information(this,"success","success");
      }
      @

      i always use this.
      and if your directory is protected , change the db file directory for testing

      If You Want You Can!

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tim.sullivan
        wrote on 5 Jun 2012, 16:52 last edited by
        #3

        I get a successful connection from this:
        @
        #define DBFILEPATH "/Volumes/Inventory/inventory.db"
        (...)
        db = new QSqlDatabase();
        *db = QSqlDatabase::addDatabase("QSQLITE");
        QFileInfo info1(DBFILEPATH);
        if (info1.isFile())
        {
        qDebug("DBFILEPATH points to a valid file.");
        }
        else
        qDebug("DBFILEPATH does not point to a valid file");
        db->setDatabaseName(DBFILEPATH);
        db->open();
        (...)
        @

        Just FYI abbas, this code doesn't really check if the database is open:
        @
        QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
        mydb.setDatabaseName(/your sqlite database file/);
        if (mydb.open())
        {
        (...)
        }
        @
        the .open() will cause the database to open at the location specified regardless of its previous existence. It will create a new database at the path if an old one by that name doesn't exist, without throwing an error.

        1 Reply Last reply
        0

        2/3

        5 Jun 2012, 08:01

        • Login

        • Login or register to search.
        2 out of 3
        • First post
          2/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved