(solved) Qt 5.2: Problem deploying sqlite database to my android device



  • Hi,

    i have problems deploying my sqlite database to an android device.
    i tryed to did it "as described here.":http://qt-project.org/doc/qt-5/platform-notes-android.html
    The database is located in a subdirectory called database/MyDatabase.db in my project folder.

    I added the following lines in .pro file:
    @android {
    folder_01.source = database
    folder_01.target = .
    DEPLOYMENTFOLDERS = folder_01
    }@

    This works and i can see the database folder including the file MyDatabase.db in the android-build/assets folder.

    In code i try to open the database like this:
    @... setDatabaseName("assets:/database/MyDatabase.db");@

    But always getting the error:
    out of memory Error opening database

    It seems somehow the assets:/database/MyDatabase.db is not working....

    Any ideas?

    Greetings,
    Nando



  • I can see you set database name but what about loading a driver and opening it.
    @
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("assets:/database/MyDatabase.db");
    db.open();@



  • Hi, yes i do this like this:
    The code works fine on Desktop:

    @
    const QString type("QSQLITE");

    if(!QSqlDatabase::isDriverAvailable(type))
    {
        qCritical() << type << " database module not available.";
        return false;
    }
    
    m_DB = QSqlDatabase::addDatabase(type);
    m_databasePath = "assets:/database/MyDatabase.db";
    m_DB.setDatabaseName(m_databasePath);
    if(!m_DB.open())
    {
        qCritical() << "couldn't connect to database Error[" <<
                       m_DB.lastError().text() << "]";
        return false;
    }
    qDebug() << "succsessfully connected to database " << m_databasePath;
    

    @

    [quote author="kolegs" date="1389637543"]I can see you set database name but what about loading a driver and opening it.
    @
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("assets:/database/MyDatabase.db");
    db.open();@[/quote]



  • I'm not sure but aren't assets files readonly, and I belive database need to be readwrite. I use the code below to copy file first and the open it:
    @QFile dfile("assets:/db/database-last.db3");
    if (dfile.exists())
    {
    dfile.copy("./database-last.db3");
    QFile::setPermissions("./database-last.db3",QFile::WriteOwner | QFile::ReadOwner);
    }@
    @QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("./database-last.db3");
    db.open();@

    and in my .pro file
    @deployment.files += database-last.db3
    deployment.path = /assets/db
    INSTALLS += deployment
    @



  • Yo are the man!!!! ;-)
    Thank you very much this works great!!!

    [quote author="kolegs" date="1389638889"]I'm not sure but aren't assets files readonly, and I belive database need to be readwrite. I use the code below to copy file first and the open it:
    @QFile dfile("assets:/db/database-last.db3");
    if (dfile.exists())
    {
    dfile.copy("./database-last.db3");
    QFile::setPermissions("./database-last.db3",QFile::WriteOwner | QFile::ReadOwner);
    }@
    @QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("./database-last.db3");
    db.open();@

    and in my .pro file
    @deployment.files += database-last.db3
    deployment.path = /assets/db
    INSTALLS += deployment
    @[/quote]



  • Excellent!

    I've been looking for this solution for two days and found it!

    thank you very much!



  • Great help!!

    Thank you!!



  • @nando76 and @kolegs!, thank you very much guys that was really helpful.



  • thanks a lot!! this problem stuck me for several days.
    BTW, there are two database files after copy it, is there a better idear for this?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.