使用LINQ访问表中的第一个元素时,SQLite的错误 [英] SQLite error when accessing first element in table using LINQ

查看:231
本文介绍了使用LINQ访问表中的第一个元素时,SQLite的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我访问使用LINQ SQLite数据库时,具有下列问题。简写代码如下所示:

I'm having the following issue when accessing a SQLite database using LINQ. Abreviated code looks like this:

...

using System.Linq
using System.Data.Linq
using System.Data.Linq.Mapping
using System.Data.SQLite

...

DataContext sqltContext= new DataContext(sqltConnection);
featureTable = sqltContext.GetTable<FeatureModel>();
count= featureTable.Count(); // count == 1



下面一行失败,错误:
SQL逻辑错误或丢失database\r\\\
unknown错误

The following line fails with error: "SQL logic error or missing database\r\nunknown error"

//currentFeature = featureTable.First(); 



不过,迭代器正常工作:

But iterator works fine:

foreach (var feature in featureTable)
{
  currentFeature = feature;
  break;
}

在这两种情况下,我得到的调试输出以下错误:

In both cases, I'm getting the following error in debug output:

SQLite error (1): near ".": syntax error

什么会导致这个错误?我希望以前有人看到这个问题,可以点我在正确的方向。但我最终可能会通过在一些点在调试器中System.Data.SQLite代码加强。

What would cause this error? I'm hoping someone has seen this issue before and can point me in the right direction. But I will probably end up stepping through the System.Data.SQLite code in the debugger at some point.

编辑:下面从GeorgeT根据意见,我添加了 sqltContext.Log = Console.Out; 。基于日志的输出,看来LINQ正在产生 SELECT TOP 命令,这是不符合SQLite的兼容。这个问题的解决似乎是为了避免首先 FirstOrDefault 的方法,或者使用类似的 的DBLinq

based on comment below from GeorgeT, I added sqltContext.Log = Console.Out;. Based on the logging output, it appears that LINQ is generating a SELECT TOP command, which isn't compatible with SQLite. The solution to this would seem to be to avoid the First or FirstOrDefault methods, or else use something like DBLinq

推荐答案

电话在。首先()方法生成使用 SELECT TOP 命令,这是不符合SQLite的兼容SQL查询。解决的办法是避免使用。首先()的方法,或者使用支持LINQ查询SQLITE,如的 的DBLinq或 LinqConnect

Calling the .First() method generates a SQL query using the SELECT TOP command, which isn't compatible with SQLite. The solution is to avoid using the .First() method, or to use a library which supports LINQ queries to SQLITE, such as DbLinq or LinqConnect.

这篇关于使用LINQ访问表中的第一个元素时,SQLite的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆