![](/img/trans.png)
[英]How can I remove a column from my SQLite table with sqlite-net-pcl that comes with Xamarin Forms?
[英]How can I read a `uuid` from a column in a sqlite3 db using sqlite-net-pcl in Xamarin Forms
我有一個我無法控制的數據庫架構(它是從桌面應用程序導出的一個 sqlite3 文件,我需要與之交互操作),其中包含某些列的 UUID。 我在 Xamarin.Forms 應用程序中使用sqlite-net-pcl
,但我無法弄清楚如何成功讀取這些列。 這是我嘗試過的:
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>()
查詢獲取 object ,則UUIDColumn
始終等於Guid.Empty
。Byte[]
; 它總是null
。string
; 它總是null
。UInt16[]
類型(GUID 可能存儲為 16 位字的 blob,所以我也嘗試了該類型) 如何使用sqlite-net-pcl
讀取uuid
類型的列中的值?
我放棄了在sqlite-net-pcl
中使用 ORM 功能並使用了以下查詢:
db.executeScalar<byte[]>('select hex(uuidcolumn) from mytable where recordid=1');
我得到的是 72 個字節,它似乎代表 Guid 的字符串表示中的 36 個 ASCII 字符(其中一個字符經常是2D
,即-
在 ASCII 集中)。 所以我認為后備存儲是一個 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.