How to use SQLite on Qt Android

  • I want to use SQLite on Qt Android 5.6, the db file is in the Qt resource and will be copied to write able location on android but it failed when copying db from resource to external path

    db = QSqlDatabase::addDatabase( "QSQLITE","MyConnection" );    
    QString filePath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
    filePath.append( "/"+dbname);
    QFile dfile(":/android/assets/"+dbname);
    if (dfile.exists())
        //QFile::setPermissions("./"+dbname,QFile::WriteOwner | QFile::ReadOwner);
        if( QFile::exists( filePath ) )
            QFile::remove( filePath );
        if( dfile.copy( filePath ) ){
            QFile::setPermissions( filePath, QFile::WriteOwner | QFile::ReadOwner );
            QMessageBox::information(0,"Info","Error copying "+dfile.fileName()+" to " +filePath);
        QMessageBox::information(0,"Info",dfile.fileName()+" is missing");

  • Qt Champions 2018

    You could call dfile.error() to see what the problem was.

Log in to reply

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