Xamarin Android SQLite

#android #sqlite #xamarin

Вопрос:

То, что я пытаюсь сделать, — это создать приложение для Android, содержащее базу данных SQLite. Я добавляю записи в базу данных SQLite на своем ПК (скажем, PCCopy). Я пытался перенести эту базу данных во встроенную базу данных в приложении Android, используя BinaryReader и BinaryWriter.

Создание базы данных и передача данных, похоже, работают. Локальная база данных находится в папке «Ресурсы». Установлен пакет SQLite из Nuget.

Однако, когда я пытаюсь подключиться к вновь созданной базе данных, я получаю сообщение об ошибке SQLite «не удается открыть».

Я борюсь с этой проблемой уже неделю. Пожалуйста, помогите. Я подозреваю, что двоичное чтение и запись могут быть связаны со структурой базы данных. ТИА

 public static void createDatabase()  {  try  {  var dbName = "hottots.db"; //this is the indented name of the DB in Android app  var fileName = "hottots.db"; // this is the database made locally added to assets folder  string documentDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);  var path = Path.Combine(documentDirectoryPath, dbName);  if (!File.Exists(path))  {  using (var binaryReader = new BinaryReader(Android.App.Application.Context.Assets.Open(fileName)))  {  using (var binaryWriter = new BinaryWriter(new FileStream(path, FileMode.Create)))  {  byte[] buffer = new byte[2048];  int length = 0;  while ((length = binaryReader.Read(buffer, 0, buffer.Length)) gt; 0)  {  binaryWriter.Write(buffer, 0, length);  }  }  }  }  }  catch (Exception e)  {  Console.WriteLine(e.Message);  }   }  public static SQLiteConnection getDBConnection()  {  var dbName = "hottots.db";  string documentDirectoryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);  var path = Path.Combine(documentDirectoryPath, dbName);  string connString = string.Format("Data Source= ", path);  SQLiteConnection conn = new SQLiteConnection(connString);  return conn;  }