【问题标题】:SQLite-net PCL; C# Int64/long to SQLite INTEGERSQLite-net PCL; C# Int64/long 到 SQLite INTEGER
【发布时间】:2015-07-18 03:54:00
【问题描述】:

我使用 Xamarin for Visual Studio 2013 和 SQLite-net PCL 版本 1.0.11 (https://www.nuget.org/packages/sqlite-net-pcl/)。

基本上,我有兴趣在 SQLite 表中存储大于 2,147,483,647 的数值,其中字段/列通过 ORM 对类进行 [PrimaryKey, AutoIncrement] 装饰。

下面的类确实创建了表,并且插入值(行)工作正常:

public class Patient
{
    [PrimaryKey, AutoIncrement, Column("PATIENT_NBR")]
    public int PatientNumber { get; set; }
    ...
}

当“long”或“Int64”与 PrimaryKey 和 AutoIncrement 结合使用时,以下两个类不会创建表:

public class Patient
{
    [PrimaryKey, AutoIncrement, Column("PATIENT_NBR")]
    public long PatientNumber { get; set; }
    ...
}

public class Patient
{
    [PrimaryKey, AutoIncrement, Column("PATIENT_NBR")]
    public Int64 PatientNumber { get; set; }
    ...
}

在上面的最后两个示例中 - 运行 CreateTable 时:

public class CreateDatabase
{
    private readonly SQLiteConnection conn;
    public CreateDatabase()
    {
        try
        {
            conn = DependencyService.Get<ISQLite>().GetConnection(Constants.DatabaseName);
            conn.CreateTable<Patient>();

            if (conn.Table<Patient>().Count() == 0)
            {
                conn.Insert(new Patient { PatientCreateDate = DateTime.UtcNow, PatientFirstName = "John", PatientLastName = "Doe" });
                conn.Insert(new Patient { PatientCreateDate = DateTime.UtcNow, PatientFirstName = "Jane", PatientLastName = "Doe" });
            }
        }
        catch (Exception ex)
        {
            throw new Exception(string.Format("Create SQLite Connection. Error: {0}.", ex.Message));
        }
    }
}

我收到异常: ex.Message:“AUTOINCREMENT 只允许在 INTEGER PRIMARY KEY 上”

如果我从 PatientNumber 的装饰中删除 AutoIncrement,它将很好地创建表,但随后我遇到约束错误,因为插入 PatientNumber 的值始终为 0(因为未使用 AutoIncrement),并且该字段被定义为 PrimaryKey 并且必须是唯一的。

基于为 SQLite-net 打开的问题:

https://github.com/praeclarum/sqlite-net/issues/227

它提到了以下内容:

https://github.com/praeclarum/sqlite-net/pull/345

(很抱歉上面的两个链接 - 它不会让我发布它们)

在上面的最后一个链接中,它提到了一种解决方法:

conn.ExtraTypeMappings.Add(typeof(int64), "integer")

但我不确定在哪里添加这个。我尝试在分配“conn”的位置下方添加它,但出现以下编译器错误: “SQLite.SQLiteConnection 不包含“ExtraTypeMapping”的定义。

我的理解是,虽然 SQLite 可以存储最大为 9223372036854775807 的数值,但我会受到“Int”或“Int32”类型的 C# 类的修饰的限制,因为它的最大值是 2,147,483,647。

是我遗漏了什么,还是我误解了什么??

任何帮助将不胜感激。

【问题讨论】:

    标签: visual-studio-2013 orm xamarin portable-class-library sqlite-net


    【解决方案1】:

    您是否混淆了您想要的 PCL? SQLite-net vs SQLite.net vs ?那里有大量的 sqlite 变体。 ;-)

    您正在提取 SQLite-net PC nuget,因此获得了 2015 年 1 月 22 日发布的 SQLite-net PCL 1.0.11

    您引用的补丁已于 2015 年 3 月 4 日合并:

    praeclarum merged commit 1638293 into praeclarum:master on Mar 4
    

    如果那是您想要的库,请拉出 master 分支(头部或直到该提交)并构建/使用它而不是 Nuget。

    或者使用 SqLiteConnection.ExtraTypeMappings,也许您正在寻找 oysteinkrog/SQLite.Net-PCL 版本?:

    Line 94 of SqLiteConnection.cs

    【讨论】:

    • 感谢您的回答。我使用了错误的 PCL。
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 2015-08-24
    • 2019-07-01
    • 2017-11-05
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 2020-03-24
    相关资源
    最近更新 更多