0

我有一个我无法控制的数据库架构(它是从桌面应用程序导出的一个 sqlite3 文件,我需要与之交互操作),其中包含某些列的 UUID。我sqlite-net-pcl在 Xamarin.Forms 应用程序中使用,我无法弄清楚如何成功读取这些列。这是我尝试过的:

  • 使用 sqlite3 命令行,我已经确认架构具有uuid相关列的类型,并且使用select count(distinct uuidcolumn) from mytable;我已经确认每一行都有值。(该列可以为空,这与下面的代码片段相关,但实际上所有行都具有非空值)
  • 我有这个模型对象:
namespace brahms.Model
{
    [Table("mytable")]
    public class MyTable
    {
        [Column("uuidcolumn")]
        public Guid UUIDColumn { get; }

        [PrimaryKey, AutoIncrement, NotNull]
        [Column("recordid")]
        public int RecordID { get; set; }
    }
}
  • database.Query<MyTable>()如果我使用查询获取对象,UUIDColumn始终等于Guid.Empty.
  • 我尝试将类定义中的类型切换为Byte[]; 它总是null
  • 我尝试将类定义中的类型切换为string; 它总是null
  • 同样适用于UInt16[]类型(GUID 可能存储为 16 位字的 blob,所以我也尝试了该类型)

如何使用 读取uuid-typed 列中的值sqlite-net-pcl

4

1 回答 1

0

我放弃了使用 ORM 功能sqlite-net-pcl并使用了这个查询:

db.executeScalar<byte[]>('select hex(uuidcolumn) from mytable where recordid=1');

我得到的是 72 个字节,它似乎代表 Guid 的字符串表示中的 36 个 ASCII 字符(其中一个字符经常是2DASCII-集中的字符)。所以我认为后备存储是一个 blob,但它存储的是 Guid 的文本表示,这很奇怪,但我可以从这里重建 Guid。

使用这个答案并将那个 blob 作为一个字符串,我最终得到了这个实现:

        public Guid GetUUIDColumn()
        {
            string dbRep = _database.ExecuteScalar<string>("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);  
        }
于 2021-02-24T14:18:52.150 回答