【问题标题】:Copy SQLite database to an android device C#将 SQLite 数据库复制到 android 设备 C#
【发布时间】:2016-05-23 06:34:41
【问题描述】:

我正在使用 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);
        }
    }

【问题讨论】:

    标签: java c# android sqlite xamarin.android


    【解决方案1】:

    如果您希望将 db 复制到设备上,我建议将其放在 assets 文件夹中,然后您可以运行以下代码...

        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";
            });
        }
    

    【讨论】:

      【解决方案2】:
      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();
                  }
              }
      

      如下图所示获取安卓设备存储路径

      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 权限。

      【讨论】:

      • 而且你每次都会复制它。
      • 复制前检查数据库是否可用
      • 我认为最好将此检查添加到ExtractDB func,否则有人会复制此片段,并且每次都会复制db。
      猜你喜欢
      • 2015-08-05
      • 2011-08-26
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      相关资源
      最近更新 更多