Xamarin forms and SQLIte database
-
I have an existing sqlite database in the android project under Assets. I use the following to copy the database to the application personal folder
public async Task GetDBPathAndCreateIfNotExists()
{
string databaseName = "DBQDive.db3";
var docFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var dbFile = Path.Combine(docFolder, databaseName); // FILE NAME TO USE WHEN COPIEDif (!File.Exists(dbFile)) { FileStream writeStream = new FileStream(dbFile, FileMode.OpenOrCreate, FileAccess.Write); await MainActivity.Instance.Assets.Open(databaseName).CopyToAsync(writeStream); } return dbFile; }
The problem is that even after uninstalling the app from the phone
if (!File.Exists(dbFile))
ALWAYS find a database file and therefore never copies the new database because the database has been created by a previous installation. How can I detect if this is the first time the app is run so I can delete the database before it should be copied over?
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
-
I have an existing sqlite database in the android project under Assets. I use the following to copy the database to the application personal folder
public async Task GetDBPathAndCreateIfNotExists()
{
string databaseName = "DBQDive.db3";
var docFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var dbFile = Path.Combine(docFolder, databaseName); // FILE NAME TO USE WHEN COPIEDif (!File.Exists(dbFile)) { FileStream writeStream = new FileStream(dbFile, FileMode.OpenOrCreate, FileAccess.Write); await MainActivity.Instance.Assets.Open(databaseName).CopyToAsync(writeStream); } return dbFile; }
The problem is that even after uninstalling the app from the phone
if (!File.Exists(dbFile))
ALWAYS find a database file and therefore never copies the new database because the database has been created by a previous installation. How can I detect if this is the first time the app is run so I can delete the database before it should be copied over?
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
Just a guess, but when you reinstall the app, the allowBackup element in the manifest may be restoring your database.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Just a guess, but when you reinstall the app, the allowBackup element in the manifest may be restoring your database.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
Thanks for the hint - had nothing to do with the problem :sigh: The issue was with precedence, I was creating a SQLite database in the DAL base class BEFORE copying over the existing DB.
new SQLiteAsyncConnection(FilePath);
This actually creates an empty database in that location and it was called prior to the copy from android. Last major issue nailed before I can deploy to the UAT people Yah!!!
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP