dimanche 1 mai 2016

How to use SQLite on Qt Android

I want to use SQLite on Qt Android 5.6, here is my code

db = QSqlDatabase::addDatabase( "QSQLITE","MyConnection" );    
QString filePath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
filePath.append( "/"+dbname);

QFile dfile(":/android/assets/"+dbname);

if (dfile.exists())
{
    //dfile.copy("./"+dbname);
    //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 );
    }else{
        QMessageBox::information(0,"Info","Error copying "+dfile.fileName()+" to " +filePath);
    }

}else{
    QMessageBox::information(0,"Info",dfile.fileName()+" is missing");
}

db.setDatabaseName(filePath);

the db file is in the Qt resource and will be copied to write able location on android but it failed on this:

if( dfile.copy( filePath ) ){

Aucun commentaire:

Enregistrer un commentaire