Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Mobile
  4. Xamarin forms and SQLIte database

Xamarin forms and SQLIte database

Scheduled Pinned Locked Moved Mobile
questionandroidmobiledatabasesqlite
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mycroft Holmes
    wrote on last edited by
    #1

    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 COPIED

    		if (!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

    D 1 Reply Last reply
    0
    • M Mycroft Holmes

      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 COPIED

      		if (!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

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • D David Crow

        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

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups