gpt4 book ai didi

c# - SQLite 选择 GUID 中的 ID

转载 作者:行者123 更新时间:2023-12-02 00:14:15 24 4
gpt4 key购买 nike

因此,我尝试从表中选择 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'
)

但我收到错误:

unrecognized token: "X'b06783c2-da62-4292-8bdc-f7915c00b2db'"

最佳答案

好吧,我明白了。

不存在Guid这样的东西。在 Sqlite 中,因此它存储为 blob这意味着当您尝试查询给定的 Guid 时您需要将您的 guid 转换为 Hexidecimal string Guid 的表示作为Byte 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();
}

关于c# - SQLite 选择 GUID 中的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46602950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com