データの種類Data types
SQLite には、INTEGER、REAL、TEXT、BLOB の 4 つのプリミティブ データ型のみが存在します。SQLite only has four primitive data types: INTEGER, REAL, TEXT, and BLOB. データベースの値が object
として返される API では、この 4 つの型のいずれかのみが返されます。APIs that return database values as an object
will only ever return one of these four types. Microsoft.Data.Sqlite では他の .NET 型がサポートされていますが、最終的には、それらの型と 4 つのいずれかのプリミティブ型との間で値が強制的に変換されます。Additional .NET types are supported by Microsoft.Data.Sqlite, but values are ultimately coerced between these types and one of the four primitive types.
.NET.NET | SQLiteSQLite | RemarksRemarks |
---|---|---|
ブール型Boolean | INTEGERINTEGER | 0 または 1 0 or 1 |
ByteByte | INTEGERINTEGER | |
Byte[]Byte[] | BLOBBLOB | |
CharChar | TEXTTEXT | UTF-8UTF-8 |
DateTimeDateTime | TEXTTEXT | yyyy-MM-dd HH:mm:ss.FFFFFFFyyyy-MM-dd HH:mm:ss.FFFFFFF |
DateTimeOffsetDateTimeOffset | TEXTTEXT | yyyy-MM-dd HH:mm:ss.FFFFFFFzzzyyyy-MM-dd HH:mm:ss.FFFFFFFzzz |
Decimal (10 進数型)Decimal | TEXTTEXT | 0.0########################### 形式。0.0########################### format. REAL は情報の損失を伴います。REAL would be lossy. |
DoubleDouble | realREAL | |
GUIDGuid | TEXTTEXT | 00000000-0000-0000-0000-00000000000000000000-0000-0000-0000-000000000000 |
Int16Int16 | INTEGERINTEGER | |
Int32Int32 | INTEGERINTEGER | |
Int64Int64 | INTEGERINTEGER | |
SByteSByte | INTEGERINTEGER | |
SingleSingle | realREAL | |
StringString | TEXTTEXT | UTF-8UTF-8 |
TimeSpanTimeSpan | TEXTTEXT | d.hh:mm:ss.fffffffd.hh:mm:ss.fffffff |
UInt16UInt16 | INTEGERINTEGER | |
UInt32UInt32 | INTEGERINTEGER | |
UInt64UInt64 | INTEGERINTEGER | 大きな値のオーバーフローLarge values overflow |
代替型Alternative types
一部の .NET 型は、代替の SQLite 型から読み取ることができます。Some .NET types can be read from alternative SQLite types. これらの代替型を使用するようにパラメーターを構成することもできます。Parameters can also be configured to use these alternative types. 詳しくは、「パラメーター」をご覧ください。For more information, see Parameters.
.NET.NET | SQLiteSQLite | RemarksRemarks |
---|---|---|
CharChar | INTEGERINTEGER | UTF-16UTF-16 |
DateTimeDateTime | realREAL | ユリウス日の値Julian day value |
DateTimeOffsetDateTimeOffset | realREAL | ユリウス日の値Julian day value |
GUIDGuid | BLOBBLOB | |
TimeSpanTimeSpan | realREAL | 日数In days |
たとえば、次のクエリでは、結果セット内の REAL 列から TimeSpan 値が読み取られます。For example, the following query reads a TimeSpan value from a REAL column in the result set.
command.CommandText =
@"
SELECT name, julianday(finished) - julianday(started) AS length
FROM task
WHERE finished IS NOT NULL
";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var name = reader.GetString(0);
var length = reader.GetTimeSpan(1);
Console.WriteLine($"'{name}' took {length}.");
}
}
列の型Column types
SQLite では、動的な型システムが使用されており、値の型がその格納先の列ではなく値自体に関連付けられています。SQLite uses a dynamic type system where the type of a value is associated with the value itself and not the column where it's stored. 列の型名には任意の名前を使用できます。You're free to use whatever column type name you want. Microsoft.Data.Sqlite ではそれらの名前に追加のセマンティクスは適用されません。Microsoft.Data.Sqlite won't apply any additional semantics to these names.
列の型名は、型のアフィニティに影響します。The column type name does have an impact on the type affinity. 一般的な注意事項として、列の型 STRING を使用すると、値に対して INTEGER または REAL への変換が試みられ、予期しない結果を招く可能性があります。One common gotcha is that using a column type of STRING will try to convert values to INTEGER or REAL, which can lead to unexpected results. INTEGER、REAL、TEXT、BLOB の 4 つのプリミティブ SQLite 型名だけを使用することをお勧めします。We recommend only using the four primitive SQLite type names: INTEGER, REAL, TEXT, and BLOB.
SQLite では、長さ、有効桁数、小数点以下桁数などの型のファセットを指定できますが、これらはデータベース エンジンによって適用されません。SQLite allows you to specify type facets like length, precision, and scale, but they are not enforced by the database engine. これらは使用しているアプリによって適用されます。Your app is responsible for enforcing these.