【问题标题】:Error using System.Data.Linq.Mapping and auto incrementing the primary key in a sqlite db使用 System.Data.Linq.Mapping 和自动递增 sqlite 数据库中的主键时出错
【发布时间】:2015-10-18 09:18:47
【问题描述】:

我正在使用SQLiteSystem.Data.Linq.Mapping。我在使用 linq 映射属性 IsDbGenerated = true 时遇到了 id AUTOINCREMENT 字段的问题。

创建表的语法。我试过这个有/没有AUTOINCREMENT

CREATE TABLE [TestTable] ([id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,[title] TEXT  NULL)

我的表类:

[Table(Name = "TestTable")]
public class TestTable
{
    [Column(IsPrimaryKey = true, IsDbGenerated =true)]
    public int id { get; set; }

    [Column]
    public string title { get; set; }
}

我是这样称呼它的。提交时出现错误,我将在此示例下方粘贴错误。需要注意的一件事是,如果我取出上面的IsDbGenerated =true 并手动输入id,它确实可以很好地插入,但我希望它可以插入AUTOINCREMENT,并且由于某种原因IsDbGenerated=true 正在杀死插入。寻求一些指导。

static void Main(string[] args)
{
    string connectionString = @"DbLinqProvider=Sqlite;Data Source = c:\pathToDB\test.s3db";
    SQLiteConnection connection = new SQLiteConnection(connectionString);
    DataContext db = new DataContext(connection);
    db.Log = new System.IO.StreamWriter(@"c:\pathToDB\mylog.log") { AutoFlush = true };

    var com = db.GetTable<TestTable>();
    com.InsertOnSubmit(new TestTable {title = "asdf2" });
    try {
        db.SubmitChanges();
    }
    catch(SQLiteException e)
    {
        Console.WriteLine(e.Data.ToString());
        Console.WriteLine(e.ErrorCode);
        Console.WriteLine(e.HelpLink);
        Console.WriteLine(e.InnerException);
        Console.WriteLine(e.Message);
        Console.WriteLine(e.StackTrace);
        Console.WriteLine(e.TargetSite);
        Console.WriteLine(e.ToString());
    }
    foreach (var TestTable in com)
    {
        Console.WriteLine("TestTable: {0} {1}", TestTable.id, TestTable.title);
    }
    Console.ReadKey();
}

错误信息:

SQL 逻辑错误或缺少数据库\r\nnear \"SELECT\": 语法错误

堆栈跟踪:

在 System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain)\r\n 在 System.Data.SQLite.SQLiteCommand.BuildNextCommand()\r\n at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 索引)\r\n at System.Data.SQLite.SQLiteDataReader.NextResult()\r\n 在 System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior 行为)\r\n 在 System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior 行为)\r\n 在 System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior 行为)\r\n 在 System.Data.Common.DbCommand.ExecuteReader()\r\n
在 System.Data.Linq.SqlClient.SqlProvider.Execute(表达式查询, QueryInfo queryInfo, IObjectReaderFactory 工厂, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object 最后一个结果)\r\n 在 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(表达式查询, QueryInfo[] queryInfos, IObjectReaderFactory 工厂, Object[] userArguments, ICompiledSubQuery[] subQueries)\r\n at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表达式 查询)\r\n 在 System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject 项目)\r\n 在 System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject 项目)\r\n 在 System.Data.Linq.ChangeProcessor.SubmitChanges(冲突模式 故障模式)\r\n 在 System.Data.Linq.DataContext.SubmitChanges(冲突模式 failureMode)\r\n 在 System.Data.Linq.DataContext.SubmitChanges()\r\n 在 SqlLinq.Program.Main(String[] args) 在 Program.cs:line 29"

这是我在日志输出中看到的:

INSERT INTO [company]([title])
VALUES (@p0)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input String (Size = 4000; Prec = 0; Scale = 0) [asdf2]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.6.81.0

SELECT [t0].[id], [t0].[title]
FROM [company] AS [t0]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.6.81.0

【问题讨论】:

    标签: c# sql linq sqlite system.data.sqlite


    【解决方案1】:

    根据 SQLite 文档 (A column declared INTEGER PRIMARY KEY will AUTOINCREMENT.),只需在表创建中删除 AUTOINCREMENT,编写 integer primary key 就足够了。 SQLite 会自动增加您的 ids

    sqlite_cmd.CommandText = "CREATE TABLE [TestTable] ([id] INTEGER PRIMARY KEY NOT NULL , [title] TEXT)";
    

    另外,您不需要在TestTable 类中设置IsDbGenerated = true,也不需要手动输入id,只需插入title 即可插入:

    com.InsertOnSubmit(new TestTable { title = "asdf2" });//will automatically increment id.
    

    编辑:您的 TestTable 现在应该如下所示:

    [Table(Name = "TestTable")]
    public class TestTable
    {
        [Column(IsPrimaryKey = true)]
        public int? id { get; set; }
    
        [Column]
        public string title { get; set; }
    }
    

    SQLite 管理器中的结果:

    【讨论】:

    • 我刚才试了这个方法。我使用您的 sql 示例重新创建了表并删除了 IsDbGenerated 属性。它确实输入了为 id 添加 0 的第一条记录。第二次尝试失败,错误与我的描述相同。这与我之前看到的行为相同。
    • 将您的 id 更改为 Nullable,如下所示:public int? id { get; set; }
    【解决方案2】:

    如何创建 AUTOINCREMENT 字段

    简答:声明为 INTEGER PRIMARY KEY 的列将自动递增。

    更长的答案:如果您将表的列声明为 INTEGER PRIMARY KEY,那么每当您在表的该列中插入 NULL 时,NULL 都会自动转换为整数,即比表中所有其他行的该列的最大值大 1,如果表为空,则为 1。或者,如果最大的现有整数密钥 9223372036854775807 正在使用中,则随机选择未使用的密钥值。例如,假设您有一个这样的表:

    CREATE TABLE t1(
      a INTEGER PRIMARY KEY,
      b INTEGER
    );
    

    有了这张表,声明

    INSERT INTO t1 VALUES(NULL,123);
    

    在逻辑上相当于说:

    INSERT INTO t1 VALUES((SELECT max(a) FROM t1)+1,123);
    

    有一个名为sqlite3_last_insert_rowid() 的函数将返回最近插入操作的整数键。

    注意整数键比最大键大一 在插入之前的表中。新密钥将是唯一的 当前在表中的所有键,但它可能与 之前已从表中删除。创建密钥 在表的整个生命周期中唯一,添加 AUTOINCREMENT 关键字 INTEGER PRIMARY KEY 声明。然后选择的密钥将是 比该表中曾经存在的最大键多一个。如果 最大可能的键以前存在于该表中,然后 INSERT 将失败并返回 SQLITE_FULL 错误代码。

    参考资料:

    Autoincrement in SQLite

    How to create Autoincrement Field ?

    SO post dealing with Autoincrement in SQLite

    【讨论】:

    • 我很可能在上面遗漏了,但我没有看到关于如何使用 linq 执行此操作的参考。当使用直接 sql 而不使用 linq 时,我能够毫无问题地处理自动增量。 (System.Data.Linq.Mapping
    【解决方案3】:

    SQLLite 不适用于自动增量值的 Linq 命令 此命令产生错误

    SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
    

    你只有两种方式:

    1. 不要将 Linq 用于 SQLLite。使用一些第三方解决方案,或者您的 自己的命令。

    2. 使用另一种方式来修改您的 ID,因为它是由 [utility] 编写的

    第一个更好,因为还有其他无效的 SQL 语句示例通过 Linq 传递给 sqlite。

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 1970-01-01
      • 2014-02-20
      • 2013-04-06
      • 2013-04-18
      • 2023-03-24
      • 2013-12-03
      • 2010-10-25
      • 1970-01-01
      相关资源
      最近更新 更多