我建議你從驅動程序無關SQLite documentation on the subject開始。它解釋了布爾值應存儲的方式,以及不同的日期時間序列化方案。
有關更多詳細信息,System.Data.SQLite是開源的,儘管圍繞某些邊緣有點瑣碎,但通常很容易閱讀。
例如,在SQLiteDataReader.cs的GetValue()
方法(這是實現的ADO.NET IDataReader
接口的一部分)調用名爲GetSQLiteType()
方法,那麼不依賴於一些連接國旗多一點的自動檢測。
GetSQLiteType()
和朋友們都回到SQLiteConvert這個類,它可以進行實際的類型轉換和檢測。轉換都是在那裏定義的(從大約一半的時間開始,在很多日期操縱助手之後)。最終你達到這個功能,這是特別重要的,以你的問題:
internal static TypeAffinity TypeToAffinity(Type typ)
{
TypeCode tc = Type.GetTypeCode(typ);
if (tc == TypeCode.Object)
{
if (typ == typeof(byte[]) || typ == typeof(Guid))
return TypeAffinity.Blob;
else
return TypeAffinity.Text;
}
return _typecodeAffinities[(int)tc];
}
private static TypeAffinity[] _typecodeAffinities = {
TypeAffinity.Null, // Empty (0)
TypeAffinity.Blob, // Object (1)
TypeAffinity.Null, // DBNull (2)
TypeAffinity.Int64, // Boolean (3)
TypeAffinity.Int64, // Char (4)
TypeAffinity.Int64, // SByte (5)
TypeAffinity.Int64, // Byte (6)
TypeAffinity.Int64, // Int16 (7)
TypeAffinity.Int64, // UInt16 (8)
TypeAffinity.Int64, // Int32 (9)
TypeAffinity.Int64, // UInt32 (10)
TypeAffinity.Int64, // Int64 (11)
TypeAffinity.Int64, // UInt64 (12)
TypeAffinity.Double, // Single (13)
TypeAffinity.Double, // Double (14)
TypeAffinity.Double, // Decimal (15)
TypeAffinity.DateTime, // DateTime (16)
TypeAffinity.Null, // ?? (17)
TypeAffinity.Text // String (18)
};
一般而言,整數類型將得到正確映射到的SQLite的(64位)整數和背部,並同上字符串。 byte[]
數組和Guid
s也將透明地工作,雖然兩者都存儲爲斑點。布爾值被映射爲1(真)和0(假)整數。並且所有SQLite datetime表示都受支持,以及更多:請參閱SQLite3.cs中的Bind_DateTime()
方法。
哇,現在我們正在談論。謝謝。我剛開始做一些測試(將輸入的參數插入到沒有親和力的列中),以反向設計行爲。您剛剛爲我節省了一些寶貴的時間:-) – misha256
在相關說明中,我剛剛發現「CREATE TABLE」SQL *中指定的數據類型對System.Data.SQLite *有意義。定義爲INT16的列對SQLite本身有一些意義(你得到了INTEGER Affinity)*,但是* System.Data.SQLite會注意到INT16的定義,並且如果可以的話,會解析爲'Int16'。這一切都是非常有價值的信息。我很想在某處寫博客...... – misha256
@ misha256:對下一個人來說可能是個好主意:-)我承認閱讀源代碼並不像寫得好的文檔那樣愉快! – Cameron