没有这样的表错误。 SQLite + Xamarin.android

时间:2017-05-15 14:00:39

标签: c# sqlite xamarin.android

我是xamarin android的新手。我使用DB Browser for SQLite在外部创建了SQLite数据库,并将其添加到我现有的android项目中。但我得到的例外是'没有这样的表:员工'

我遵循以下步骤。

  • 在Visual Studio 2015中创建了新项目(Android>空白应用程序(Android))。
  • 使用DB Browser for SQLite创建数据库。下面是架构

Database Schema

  • 将文件保存为Assets文件夹中的'EmployeeDB.db3。
  • 将此文件的构建操作设置为“AndroidAsset”
  • 在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();
        }
    }
    

在运行时,我收到异常消息。

  

“没有这样的表:员工”

我错过了什么吗?任何帮助,将不胜感激。感谢。

0 个答案:

没有答案