c# - 尝试创建表时在 Xamarin.Android 中抛出 SQLite.SQLiteException

标签 c# android sqlite xamarin.ios xamarin

我正在尝试使用 Xamarin.Android 创建一个简单的本地 SQLite 数据库。 代码是:

string folder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
SQLiteConnectiondb = new SQLiteConnection (Path.Combine (folder, "Experimental.db"));
db.CreateTable<Employee>();

我的员工类是:

[Table("Employees")]
    public class Employee
    {
        [PrimaryKey, AutoIncrement]
        int Id { get; set; }

        string Name { get; set; }

    }

每当代码到达 db.CreateTable 时我得到的异常是:

 SQLite.SQLiteException: near ")": syntax error
  at at SQLite.SQLite3.Prepare2 (intptr,string) <IL 0x0002c, 0x00160>
  at at SQLite.SQLiteCommand.Prepare () <IL 0x00011, 0x0008f>
  at at SQLite.SQLiteCommand.ExecuteNonQuery () <IL 0x00013, 0x000b3>
  at at SQLite.SQLiteConnection.Execute (string,object[]) <IL 0x00041, 0x001ab>
  at at SQLite.SQLiteConnection.CreateTable (System.Type,SQLite.CreateFlags) <IL 0x000a4, 0x005cb>
  at at SQLite.SQLiteConnection.CreateTable<Core.Employee> (SQLite.CreateFlags) <0x00063>

在我没有经验的人看来,这看起来像是与 SQLite 本身有关的问题。有其他人遇到过这个问题吗?如果遇到过 - 您的解决方法是什么?

最佳答案

公开 Employee 类的属性,以便 SQLite 在为您的表创建列时可以看到它们:

[Table("Employees")]
public class Employee
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public string Name { get; set; }

}

关于c# - 尝试创建表时在 Xamarin.Android 中抛出 SQLite.SQLiteException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26243131/

相关文章:

c# - 有没有办法在正则表达式中执行动态替换?

android - 使用 Proguard 删除 Google Play 服务库中未使用的类

php - 我如何导入 SQLite db3 文件

python - 如何向多对多关系添加附加信息?

c# - 使用 LINQ 合并对象列表

c# - Xamarin – 错误修复 “The Aapt task failed unexpectedly”

c# - 在 c# 和 winrt 中将流保存到文件

android - 在折叠工具栏布局中添加 FloatingActionButton

android 检测触摸

python - 为什么插入速度随着数据库的增长而减慢?