Solved How to record a database in Android
-
Hi!
How to write in Qt SQLite database in Android?
If you just copy from the resources, this base can not be changed.
If you create via code, you cannot open it (on Windows works). -
@mikeeeeee said in How to record a database in Android:
If you just copy from the resources, this base can not be changed.
It depends to which location you copy it. It needs to be writeable location.
So: where do you copy it? -
@mikeeeeee
It feels, like I answered this before ...ah yes:
https://forum.qt.io/topic/105104/how-to-add-sqlite-to-qresource -
But I determine the location of the folder and add to it the name of the database. Then I can safely download the database to disk. But it's not readable.
-
@mikeeeeee Please show your code...
-
Tried create a new, and copy. If you copy, I can not change the database.
void AppCore::copyDbInFolder()
{
requestAndroidPermissions();
//dbAdress = getDirApp() + "/dataBase.db";
//dbAdress.replace("/","\");dbAdress = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).append("/myDataBaseForBrowser.db"); if( ! QFile::exists(dbAdress)) { QFile::copy(":/dataBase/dataBase.db", dbAdress); //:/icons/close.png /*QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection db.setDatabaseName(dbAdress); db.open(); QSqlQuery query; query.exec("CREATE TABLE downloadTable (" "id INTEGER PRIMARY KEY " "NOT NULL," " nameFile TEXT," "fileUrl TEXT" ");" ); query.exec(" CREATE TABLE historyTable (" " id INTEGER PRIMARY KEY" " NOT NULL," "url TEXT," "nameSite TEXT" ");" ); query.exec("CREATE TABLE openTabTable (" "id INTEGER NOT NULL" " PRIMARY KEY AUTOINCREMENT," "url TEXT," "nameSite TEXT," "screen TEXT" ");" ); db.close();*/ } //QFile f1(dbAdress); //f1.setPermissions(QFileDevice::WriteUser);
}
-
@mikeeeeee In this code you commented out the part where you access your database.
So, which part exactly does not work? -
I have a resource base. Also I tried not to copy from resources, and create a new. What I have commented? How to do it properly?
-
void AppCore::copyDbInFolder()
{
requestAndroidPermissions();
//dbAdress = getDirApp() + "/dataBase.db";
dbAdress = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).append("/myDataBaseForBrowser.db");
//dbAdress = QStandardPaths::standardLocations(QStandardPaths::DataLocation) + QDir::separator() + "dataBase.db"
//dbAdress = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).append("/myDataBaseForBrowser.db");
//QFile f2(dbAdress);
//if (f2.remove()) {"remuve file";} else {"dont remove file";};
//dbAdress.replace("/","\");
if( ! QFile::exists(dbAdress))
{
qDebug()<<"файл с базой не существует";
/QFile::copy(":/dataBase/dataBase.db", dbAdress); //:/icons/close.png
QFile f1(dbAdress);
f1.setPermissions(QFileDevice::WriteUser);/
QFile *db1 = new QFile(dbAdress);
if(db1->open(QFile::WriteOnly)){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
db.setDatabaseName(dbAdress);
db.open();
QSqlQuery query;
query.exec("CREATE TABLE downloadTable ("
"id INTEGER PRIMARY KEY "
"NOT NULL,"
" nameFile TEXT,"
"fileUrl TEXT"
");"
);
query.exec(" CREATE TABLE historyTable ("
" id INTEGER PRIMARY KEY"
" NOT NULL,"
"url TEXT,"
"nameSite TEXT"
");"
);
query.exec("CREATE TABLE openTabTable ("
"id INTEGER NOT NULL"
" PRIMARY KEY AUTOINCREMENT,"
"url TEXT,"
"nameSite TEXT,"
"screen TEXT"
");"
);
db.commit();
db.close();
}
}
else {
qDebug()<<"файл с базой уже существует";
}
if(QFile::exists(dbAdress)){qDebug()<<"файл с базой записался";} else {qDebug()<<"файл с базой не записался";}
//QFile f1(dbAdress);
//f1.setPermissions(QFileDevice::WriteUser);
} -
@mikeeeeee said in How to record a database in Android:
What I have commented?
Do you actually read the code you post here?!
Here:/*QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
I'm not going to debug your code. You don't even tell us what exactly does not work (and I already ask you which part does not work, but you once more do not answer...)!
Did copy succeed? If so what did not work?
It is really hard to help you as you're asking unclear answers without needed information and do not answer questions...And you last code again contains a lot commented out code. Why don't you clean it up to help others to help you?
-
I posted working code and marked the topic solved. If anyone needs it, they can have my code. More in it to understand it is not necessary.