我有一个我不能控制的数据库模式(它是一个从桌面应用程序导出的sqlite3文件,我需要与它进行互操作),它包含一些列的UUID。我正在使用
在Xamarin.Forms应用程序中,我不知道如何成功地读取这些列。这是我尝试过的:
使用sqlite3命令行,我已经确认了模式的类型
对于相关列,并使用
我已经确认了每一行都有值。(该列可以为空,这与下面的代码片段相关,但实际上所有行都具有非空值)
我有这个模型对象:
namespace brahms.Model
{
[Table("mytable")]
public class MyTable
{
[Column("uuidcolumn")]
public Guid UUIDColumn { get; }
[PrimaryKey, AutoIncrement, NotNull]
[Column("recordid")]
public int RecordID { get; set; }
}
}如果我使用
查询,
是
始终
等于
..。
我尝试将类定义中的类型切换为
;它总是
..。
我尝试将类定义中的类型切换为
;它总是
..。
同样的情况也适用于
类型( GUID可能存储为一个16位字的blob,所以我也尝试了这种类型)
如何读取中的值
-typed列使用
发布于 2021-02-24 22:18:52
我放弃了使用ORM特性
并使用此查询:
db.executeScalar('select hex(uuidcolumn) from mytable where recordid=1');我得到的是72个字节,它似乎表示Guid的字符串表示形式中的36个ASCII字符(每隔一段时间,其中一个字符是
,即
在ASCII集合中)。所以我认为后备存储是一个blob,但它存储了Guid的文本表示,这很奇怪,但我可以从这里重建Guid。
使用
这个答案
得到一个字符串形式的blob,我最终得到了这个实现:
public Guid GetUUIDColumn()
{
string dbRep = _database.ExecuteScalar("select hex(uuidcolumn) from mytable where recordid = ?", RecordID);
if (dbRep == null || dbRep == string.Empty) return Guid.Empty;
var bytes = new byte[dbRep.Length / 2];
// each pair of bytes represents the ASCII code (in hexadecimal) for a character in the string representation of a Guid.
for (var i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(dbRep.Substring(i * 2, 2), 16);
}
string asString = Encoding.ASCII.GetString(bytes);
return new Guid(asString);
}https://stackoverflow.com/questions/66331707
复制相似问题