Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Sqlite database access in Android app error



  • hi can someone please assist me I can not access my database in my android app, I am able to embed it in assert file I am getting the following error
    W/libAccounts.so(27144): (null):0 ((null)): QSqlDatabasePrivate::database: unable to open database: "out of memory Error opening database"

    project file :

    android
    {
    my_files.path = /assets
    my_files.files = C:/Users/Gotora/Desktop/Acc_Software/Accounts/database.db
    INSTALLS += my_files
    }

    c++ file:

    QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );

    QFile DbFile;
    QString DatabaseDataStoragePath = QStandardPaths::writableLocation(QStandardPaths::StandardLocation::AppDataLocation);
    DbFile.setFileName("assets:/database.sqlite");
    DbFile.copy(DatabaseDataStoragePath + "/database.sqlite");
    QFile::setPermissions(DatabaseDataStoragePath + "/database.sqlite", QFile::WriteOwner | QFile::ReadOwner);
    db.setDatabaseName( DatabaseDataStoragePath);```


  • Lifetime Qt Champion

    Hi,

    You should check the exact path where the database is copied and check whether you can really access it there.

    Just in case, you pass DatabaseDataStoragePath to setDatabaseName but that's the folder where the database is stored, not the database itself.



  • @SGaist

    thanks I solved this way
    in project file :
    android {
    data.files = C:/Users/Gotora/Desktop/Acc_Software/Accounts/MyDatabase.db
    data.path = /assets/database
    INSTALLS += data
    }

    in c++ file:

    QFile dfile("assets:/database/MyDatabase.db");
    QString filePath = QStandardPaths::writableLocation( QStandardPaths::StandardLocation::AppLocalDataLocation );
    filePath.append( "/MyDatabase.db");
    if (dfile.exists()) {
    if( QFile::exists( filePath ) )
    QFile::remove( filePath );

          if( dfile.copy( filePath ) )
              QFile::setPermissions( filePath, QFile::WriteOwner | QFile::ReadOwner );
    
      }
      db.setDatabaseName( filePath );

Log in to reply