【问题标题】:SQLite Select Where id in GUIDSQLite 在 GUID 中选择 Where id
【发布时间】:2017-10-06 09:47:30
【问题描述】:

所以我试图从Id in (value,value1,value2) 的表中选择所有值

其中value 是一个类型Guid

使用这个答案:Using guid in sqlite select where guid is stored in the sqlite db as binaries

我已经创建了这个 Sql 语句:

Select *
from [Imaging.ImageTag]
where ImageId in (X'b06783c2-da62-4292-8bdc-f7915c00b2db',
                  X'9bc10d55-661b-4530-b224-3fe32651cb74',
                  X'0404c0c1-65a0-4913-9e36-fd9d3e129783',
                  X'cd41ac25-9b75-4d14-bd6b-38e56013020a',
                  X'122014a5-f8bd-4c0b-8c95-b9eec4b60e7e',
                  X'fa4bad5d-42b2-40fa-bf78-4942f29f6355',
                  X'3938b974-d174-4492-9d3b-68733bef9a14',
                  X'b8ac27c4-125f-4fa9-bc91-c6c7fb83c33c',
                  X'0e10bd73-4254-4614-a112-031ce0a3d526',
                  X'd2979e7c-1cb0-40cb-9898-c57d2f4a083a',
                  X'04251d49-9cd8-40d8-9c43-a17a325ddd4b',
                  X'44e56e78-94ce-4308-815d-7d95acae6904',
                  X'481c95d1-52f7-4331-a0f0-a2754dba87f5'
                 )

但我得到了错误:

无法识别的令牌:“X'b06783c2-da62-4292-8bdc-f7915c00b2db'”

【问题讨论】:

  • 去掉-符号还能用吗?即:X'b06783c2da6242928bdcf7915c00b2db'
  • @Igor 我认为可能是它,但我测试它并返回 0 行。 (虽然它没有抛出异常)但看起来也不起作用
  • 如果它返回 0 行并且没有产生错误,那么这是正确的语法,但是您的列表中没有/只是没有匹配项。
  • 我知道那里有那些 guid 的行。我会一直用头撞它,如果我找到任何东西,我会告诉你
  • 您必须知道这些值实际存储的格式。 SELECT typeof(ImageId), max(ImageId) FROM ImageTag 的输出是什么?

标签: c# sql sqlite xamarin


【解决方案1】:

好的,我想通了。

Sqlite 中没有 Guid 这样的东西,因此它被存储为 blob 这意味着当您尝试查询给定的 Guid 时,您需要将您的 guid 转换为 Hexidecimal string 表示GuidByte array

要在C# 中执行此操作,我有代码:

query = string.Format("Select * from [TableName] where ImageId in (x'{0}')", string.Join("',x'", ids.Select(x => SqliteDatabaseExtensions.ByteArrayToString(x.ToByteArray())));

其中idsList<Guid>

其中重要的一点是:

SqliteDatabaseExtensions.ByteArrayToString(new Guid(x.ToString()).ToByteArray())

静态方法在哪里:

private static string ByteArrayToString(byte[] ba)
{
    StringBuilder hex = new StringBuilder(ba.Length * 2);
    foreach (byte b in ba)
        hex.AppendFormat("{0:x2}", b);
    return hex.ToString();
}

【讨论】:

  • 当有字节数组时,可以直接作为blob参数使用。
  • 任何还在挣扎的人,记住它的 X 而不是 x。
猜你喜欢
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
  • 2013-05-27
  • 1970-01-01
  • 2018-10-09
  • 2013-03-13
相关资源
最近更新 更多