簡體   English   中英

將SQLite數據庫復制到Android設備C#

[英]Copy SQLite database to an android device C#

我正在使用Xamarin來構建應用程序,並且我有一個現有的SQLite數據庫,我想將該數據庫復制到設備上。 問題是我發現的唯一示例代碼是在Java上以及某種程度上

無法將類型'System.IO.Stream'隱式轉換為'Java.IO.InputStream'

這是我的代碼

public void copyDataBase()
    {
        try
        {
            InputStream myInput = mContext.Assets.Open(DB_NAME);

            string outputFileName = DB_PATH + DB_NAME;
            OutputStream myOutput = new FileOutputStream(outputFileName);

            byte[] buffer = new byte[1024];
            int length;

            while ((length = myInput.Read(buffer, 0, 0)) > 0)
            {
                myOutput.Write(buffer, 0, length);
            }

            myOutput.Flush();
            myOutput.Close();
            myInput.Close();
        }
        catch (Exception e)
        {
            System.Console.WriteLine(e.Message);
        }
    }

如果要將數據庫復制到設備上,建議將其放在資產文件夾中,然后可以運行以下代碼...

    public const string databaseName = "Sample.db";
    public static async Task<string> DatabaseExists(Context context)
    {
        return await Task.Run(delegate
        {
            /**
            this code is temporary
            **/
            string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "..", databaseName);
            //File.Delete(dbPath);
            // Check if your DB has already been extracted.
            if (!File.Exists(dbPath))
            {
                using (BinaryReader br = new BinaryReader(context.Assets.Open(databaseName)))
                {
                    using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
                    {
                        byte[] buffer = new byte[2048];
                        int len = 0;
                        while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            bw.Write(buffer, 0, len);
                        }
                        bw.Close();
                        bw.Dispose();
                    }
                    br.Close();
                    br.Dispose();
                    return "success";
                }

            }
            else
            {


            }
            return "success";
        });
    }
public void ExtractDB()
        {
            var szSqliteFilename = "databasename.db3";
            var szSourcePath = new FileManager().GetLocalFilePath(szSqliteFilename);

            var szDatabaseBackupPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/databasename_Backup.db3";
            if (File.Exists(szSourcePath))
            {
                File.Copy(szSourcePath, szDatabaseBackupPath, true);
                Toast.MakeText(this, "Copied", ToastLength.Short).Show();
            }
        }

獲取到android設備存儲的路徑,如下所示

public class FileManager
    {
        public string GetLocalFilePath(string filename)
        {
            string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            return Path.Combine(path, filename);
        }
    }

您需要在清單文件中添加android.permission.WRITE_EXTERNAL_STORAGE權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM