Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Local SQLite Database in Xamarin Forms ?

How can i use a Database that i made outside Xamarin ?
I don't want to create a new Database in Xamarin i just want my app to connect to old Database ?

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭
    Is the old database on the device in one of the folders that is accessible by your app?
    If so, then yes you just open the database with the path and filename.
  • blackoutblackout Member ✭✭

    @JohnH I'm a beginner so I don't really know how to do it .
    The old Database is in the Assets File and i don't know how to give the PathFile and Filename ?
    If you can help me I'll appreciate it .. thx ..

  • ColeXColeX Member, Xamarin Team Xamurai
    edited December 2019

    The old Database is in the Assets File and i don't know how to give the PathFile and Filename ?

    AFAIK It is impossible to edit file in assets directory , you could copy the db file from assets to internal storage, check here , and then access the db file using SQLite.NET.

  • JohnHJohnH GBMember ✭✭✭✭✭
    I was about to post the same link as @ColeX! :smile:
  • blackoutblackout Member ✭✭

    I tried everyThing but it didn't work for me !
    everyTime where i need to write Asset in the code it shows me an error assets does not exist ..
    Here I'm trying to read from assets and shows me the same problem ..

    AssetManager assets = this.Assets;

    @ColeX @JohnH

  • CorwynCorwyn USMember ✭✭✭

    @blackout Following the thread, it is not clear to me what you are actually trying to accomplish. Answering how I would interpret your original query I do the following:

    Create an interface that lives in my services folder.
    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace StretchedPenny.Services
    {
        public interface IFileHelperService
        {
            void CheckDB(string filename);
    
            string GetPlatformDBPath(string filename);
        }
    }
    

    Then In my android code I implement the interface

    using System;
    using Android.App;
    using System.IO;
    using StretchedPenny.Services;
    using StretchedPenny.Droid;
    
    [assembly: Xamarin.Forms.Dependency(typeof(dbhelper))]
    namespace StretchedPenny.Droid
    {
        public class dbhelper : IFileHelperService
        {
            public dbhelper() { }
            public void CheckDB(string filename)
            {
                CopyDatabaseIfNotExists(filename);
            }
            public string GetPlatformDBPath(string fName)
            {
                string path = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                string dbPath = Path.Combine(path, fName);
                return dbPath;
            }
            private void CopyDatabaseIfNotExists(string fName)
            {
                var dbPath = GetPlatformDBPath(fName);
                var forcecopy = false;
                if (!File.Exists(dbPath) || forcecopy)
                {
                    using (var br = new BinaryReader(Application.Context.Assets.Open(fName)))
                    {
                        using (var bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
                        {
                            byte[] buffer = new byte[2048];
                            int length = 0;
                            while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                bw.Write(buffer, 0, length);
                            }
                        }
                    }
                }
            }
        }
    }
    

    This checks to see if the db already exists in a folder that I can access if not, it makes a copy there.

    Then over in the code where I am going to access the db I do the following

                DependencyService.Get<IFileHelperService>().CheckDB((db));
                string dblocation = DependencyService.Get<IFileHelperService>().GetPlatformDBPath((db));
                database = new SQLiteAsyncConnection(dblocation);
    
Sign In or Register to comment.