我是xamarin android的新手。我使用DB Browser for SQLite在外部创建了SQLite数据库,并将其添加到我现有的android项目中。但我得到的例外是'没有这样的表:员工'
我遵循以下步骤。
在MainActivity.cs中,我写了以下代码
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
employeeListView = FindViewById<ListView>(Resource.Id.listView1);
try
{
var sqliteFilename = "EmployeeDB.db3";
string libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(libraryPath, sqliteFilename);
CopyDatabaseIfNotExists(path);
conn = new SQLiteConnection(path);
var empList = GetItems();
empAdapter = new EmployeeAdapter(this, empList.ToList());
employeeListView.Adapter = empAdapter;
}
catch (Exception ex)
{
throw;
}
}
private void CopyDatabaseIfNotExists(string path)
{
if (!File.Exists(path))
{
using (var br = new BinaryReader(Application.Context.Assets.Open("EmployeeDB.db3")))
{
using (var bw = new BinaryWriter(new FileStream(path, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int length = 0;
while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, length);
}
}
}
}
}
public IEnumerable<Employee> GetItems()
{
lock (locker)
{
return (from i in conn.Table<Employee>() select i).ToList();
}
}
在运行时,我收到异常消息。
“没有这样的表:员工”
我错过了什么吗?任何帮助,将不胜感激。感谢。