日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQLite3的数据类型转载()

發(fā)布時間:2025/3/20 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLite3的数据类型转载() 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

z

SQL 數(shù)據(jù)庫引擎 (據(jù)我們所知,除 SQLite 之外的所有 SQL 數(shù)據(jù)庫引擎)都使用嚴格的靜態(tài)類型。使用靜態(tài)類型,值的類型便由它的容器 -- 存儲值的特定的列 -- 來決定。

SQLite 使用更通用的動態(tài)類型系統(tǒng)。在 SQLit 中,值的數(shù)據(jù)類型與值本身相關(guān),而不是與它的容器。SQLite 的動態(tài)類型系統(tǒng)與其它數(shù)據(jù)庫引擎的常用靜態(tài)類型系統(tǒng)是向后兼容的,在這個意義上,工作在靜態(tài)類型數(shù)據(jù)庫上的 SQL 語句應(yīng)該以同樣的方式工作在 SQLite 中。然而,SQLite 中的動態(tài)類型允許它做傳統(tǒng)的嚴格類型的數(shù)據(jù)庫所不能做的事。

?

1.0 Storage Classes and Datatypes

Each value stored in an SQLite database (or manipulated by the database engine) has one of the following storage classes:

  • NULL. The value is a NULL value.

  • INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

  • REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.

  • TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).

  • BLOB. The value is a blob of data, stored exactly as it was input.

Note that a storage class is slightly more general than a datatype. The INTEGER storage class, for example, includes 6 different integer datatypes of different lengths. This makes a difference on disk. But as soon as INTEGER values are read off of disk and into memory for processing, they are converted to the most general datatype (8-byte signed integer). And so for the most part, "storage class" is indistinguishable from "datatype" and the two terms can be used interchangeably.

Any column in an SQLite version 3 database, except an?INTEGER PRIMARY KEY?column, may be used to store a value of any storage class.

All values in SQL statements, whether they are literals embedded in SQL statement text or?parameters?bound toprecompiled SQL statements?have an implicit storage class. Under circumstances described below, the database engine may convert values between numeric storage classes (INTEGER and REAL) and TEXT during query execution.

譯者信息

1.0 存儲類型與數(shù)據(jù)類型

存儲在 SQLite 數(shù)據(jù)庫中的每個值(或是由數(shù)據(jù)庫引擎所操作的值)都有一個以下的存儲類型:

  • NULL. 值是空值。
  • INTEGER. 值是有符號整數(shù),根據(jù)值的大小以1,2,3,4,6 或8字節(jié)存儲。
  • REAL. 值是浮點數(shù),以8字節(jié) IEEE 浮點數(shù)存儲。
  • TEXT. 值是文本字符串,使用數(shù)據(jù)庫編碼(UTF-8, UTF-16BE 或 UTF-16LE)進行存儲。
  • BLOB. 值是一個數(shù)據(jù)塊,按它的輸入原樣存儲。

注意,存儲類型比數(shù)據(jù)類型更籠統(tǒng)。以 INTEGER 存儲類型為例,它包括6種不同的長度不等的整數(shù)類型,這在磁盤上是不同的。但是只要 INTEGER 值從磁盤讀取到內(nèi)存進行處理,它們就被轉(zhuǎn)換為更為一般的數(shù)據(jù)類型(8字節(jié)有符號整型)。因此在一般情況下,“存儲類型” 與 “數(shù)據(jù)類型” 沒什么差別,這兩個術(shù)語可以互換使用。

SQLite 版本3數(shù)據(jù)庫中的任何列,除了整型主鍵列,都可用于存儲任何存儲類型的值。

SQL 語句中的任何值,無論它們是嵌入到 SQL 語句中的字面量還是綁定到預(yù)編譯 SQL 語句中的參數(shù),都有一個隱含的存儲類型。在下述情況下,數(shù)據(jù)庫引擎會在執(zhí)行查詢時在數(shù)值存儲類型(INTEGER 和 REAL)和 TEXT 之間進行轉(zhuǎn)換。

1.1 Boolean Datatype

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

1.2 Date and Time Datatype

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in?Date And Time Functions?of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

  • TEXT?as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
  • REAL?as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER?as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

譯者信息

1.1 布爾類型

SQLite 并沒有單獨的布爾存儲類型,而是將布爾值存儲為整數(shù) 0 (false) 和 1 (true)。

1.2 日期和時間類型

SQLite 沒有另外的存儲類型來存儲日期和時間。SQLite 的內(nèi)置的日期和時間函數(shù)能夠?qū)⑷掌诤蜁r間存為 TEXT、REAL 或 INTEGER 值:

  • TEXT? ISO8601 字符串 ("YYYY-MM-DD HH:MM:SS.SSS")。
  • REAL?儒略日數(shù) (Julian Day Numbers),按照前公歷,自格林威治時間公元前4714年11月24日中午以來的天數(shù)。
  • INTEGER?Unix 時間,自 1970-01-01 00:00:00 UTC 以來的秒數(shù)。

應(yīng)用可以選擇這些格式中的任一種存儲日期和時間,并使用內(nèi)置的日期和時間函數(shù)在這些格式間自由轉(zhuǎn)換。

2.0 Type Affinity

In order to maximize compatibility between SQLite and other database engines, SQLite supports the concept of "type affinity" on columns. The type affinity of a column is the recommended type for data stored in that column. The important idea here is that the type is recommended, not required. Any column can still store any type of data. It is just that some columns, given the choice, will prefer to use one storage class over another. The preferred storage class for a column is called its "affinity".

Each column in an SQLite 3 database is assigned one of the following type affinities:

  • TEXT
  • NUMERIC
  • INTEGER
  • REAL
  • NONE

A column with TEXT affinity stores all data using storage classes NULL, TEXT or BLOB. If numerical data is inserted into a column with TEXT affinity it is converted into text form before being stored.

譯者信息

2.0 類型親和性

為了最大限度地提高 SQLite 和其它數(shù)據(jù)庫引擎之間的兼容性,SQLite 支持列的“類型親和性”的概念。列的類型親和性是指數(shù)據(jù)存儲于該列的推薦類型。這里重要的思想是類型是推薦的,而不是必須的。任何列仍可以存儲任何類型的數(shù)據(jù)。這只是讓一些列有選擇性地優(yōu)先使用某種存儲類型。一個列的首選存儲類型被稱為它的“親和性”。

每個 SQLite 3 數(shù)據(jù)庫中的列都歸于以下的類型親和性中的一種:

  • TEXT
  • NUMERIC
  • INTEGER
  • REAL
  • NONE

一個具有 TEXT 親和性的列使用存儲類型 NULL、 TEXT 或 BLOB 存儲所有數(shù)據(jù)。如果數(shù)值數(shù)據(jù)被插入到一個具有 TEXT 親和性的列,則數(shù)據(jù)在存儲前被轉(zhuǎn)換為文本形式。

?

A column with NUMERIC affinity may contain values using all five storage classes. When text data is inserted into a NUMERIC column, the storage class of the text is converted to INTEGER or REAL (in order of preference) if such conversion is lossless and reversible. For conversions between TEXT and REAL storage classes, SQLite considers the conversion to be lossless and reversible if the first 15 significant decimal digits of the number are preserved. If the lossless conversion of TEXT to INTEGER or REAL is not possible then the value is stored using the TEXT storage class. No attempt is made to convert NULL or BLOB values.

A string might look like a floating-point literal with a decimal point and/or exponent notation but as long as the value can be expressed as an integer, the NUMERIC affinity will convert it into an integer. Hence, the string '3.0e+5' is stored in a column with NUMERIC affinity as the integer 300000, not as the floating point value 300000.0.

譯者信息

數(shù)值親和性的列可能包含了使用所有五個存儲類的值。當插入文本數(shù)據(jù)到數(shù)值列時,該文本的存儲類型被轉(zhuǎn)換成整型或?qū)崝?shù)(按優(yōu)先級排序)如果這種轉(zhuǎn)換是無損或可逆的的話。對于文本與實數(shù)類型之間的轉(zhuǎn)換,如果前15個重要十進制數(shù)字被保留的話,SQLite認為這種轉(zhuǎn)換是無損并可逆的。如果文本不能無損地轉(zhuǎn)換成整型或?qū)崝?shù),那這個值將以文本類型存儲。不要試圖轉(zhuǎn)換NULL或BLOB值。

一個字符串可能看上去像帶有小數(shù)點和/或指數(shù)符的浮點文字,但只要這個值可以用一個整型表示,數(shù)值親和性就會把它轉(zhuǎn)換成一個整型。因此,字符串‘3.0e+5’以整型300000,而不是浮點值30000.0的形式存儲在一個數(shù)值親和性的列里。

?

A column that uses INTEGER affinity behaves the same as a column with NUMERIC affinity. The difference between INTEGER and NUMERIC affinity is only evident in a?CAST expression.

A column with REAL affinity behaves like a column with NUMERIC affinity except that it forces integer values into floating point representation. (As an internal optimization, small floating point values with no fractional component and stored in columns with REAL affinity are written to disk as integers in order to take up less space and are automatically converted back into floating point as the value is read out. This optimization is completely invisible at the SQL level and can only be detected by examining the raw bits of the database file.)

A column with affinity NONE does not prefer one storage class over another and no attempt is made to coerce data from one storage class into another.

譯者信息

一個使用整型親和性的列與具有數(shù)值親和性的列表現(xiàn)一致。只是在CAST表達式里,它們之間的區(qū)別體現(xiàn)得明顯。

除了強制將整型值轉(zhuǎn)換成浮點表示外,一個具有實數(shù)親和性的列與具有數(shù)值親和性的列表現(xiàn)一致(作為一個內(nèi)部的優(yōu)化,為了少占用空間,無小數(shù)部分且存儲在實數(shù)親和性列上的小浮點值以整型形式寫到磁盤,讀出時自動轉(zhuǎn)換回浮點值。在SQL級別,這種優(yōu)化是完全不可見的,并且只能通過檢查數(shù)據(jù)庫文件的原始比特檢測到)。

一個具有NONE親和性的列不能從一種存儲類型轉(zhuǎn)換成另一種,也不要試圖強制對它進行轉(zhuǎn)換。

?

2.1 Determination Of Column Affinity

The affinity of a column is determined by the declared type of the column, according to the following rules in the order shown:

  • If the declared type contains the string "INT" then it is assigned INTEGER affinity.

  • If the declared type of the column contains any of the strings "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity. Notice that the type VARCHAR contains the string "CHAR" and is thus assigned TEXT affinity.

  • If the declared type for a column contains the string "BLOB" or if no type is specified then the column has affinity NONE.

  • If the declared type for a column contains any of the strings "REAL", "FLOA", or "DOUB" then the column has REAL affinity.

  • Otherwise, the affinity is NUMERIC.

  • Note that the order of the rules for determining column affinity is important. A column whose declared type is "CHARINT" will match both rules 1 and 2 but the first rule takes precedence and so the column affinity will be INTEGER.

    譯者信息

    2.1 列親和性測定

    列的親和性是由它的聲明類型決定的,按照以下順序所示的規(guī)則:

    1. 如果聲明類型包含字符串“INT”,那它被指定為整型親和性;

    2. 如果列的聲明類型包含任何“CHAR”、“CLOB”或“TEXT”字符串,那么該列具有文本親和性。注意:VARCHAR類型包含“CHAR”并且被指定為文本親和性;

    3. 如果列的聲明類型包含“BLOB”或者沒有指定類型,那這列具有NONE親和性;

    4. 如果列的聲明類型包含任何“REAL”、“FLOA”或“DOUB”字符串,則該列具有實數(shù)親和性;

    5. 否則,它將具有數(shù)值親和性。

    注意:判定列親和性規(guī)則的順序是很重要的。一個具有“CHARINT”聲明類型的列將匹配規(guī)則1和2,但是規(guī)則1優(yōu)先所有該列具有整型親和性。

    ?

    2.2 Affinity Name Examples

    The following table shows how many common datatype names from more traditional SQL implementations are converted into affinities by the five rules of the previous section. This table shows only a small subset of the datatype names that SQLite will accept. Note that numeric arguments in parentheses that following the type name (ex: "VARCHAR(255)") are ignored by SQLite - SQLite does not impose any length restrictions (other than the large global?SQLITE_MAX_LENGTH?limit) on the length of strings, BLOBs or numeric values.

    Example Typenames From The
    CREATE TABLE Statement
    or CAST ExpressionResulting AffinityRule Used To Determine Affinity
    INT
    INTEGER
    TINYINT
    SMALLINT
    MEDIUMINT
    BIGINT
    UNSIGNED BIG INT
    INT2
    INT8
    INTEGER1
    CHARACTER(20)
    VARCHAR(255)
    VARYING CHARACTER(255)
    NCHAR(55)
    NATIVE CHARACTER(70)
    NVARCHAR(100)
    TEXT
    CLOB
    TEXT2
    BLOB
    no datatype specified
    NONE3
    REAL
    DOUBLE
    DOUBLE PRECISION
    FLOAT
    REAL4
    NUMERIC
    DECIMAL(10,5)
    BOOLEAN
    DATE
    DATETIME
    NUMERIC5

    Note that a declared type of "FLOATING POINT" would give INTEGER affinity, not REAL affinity, due to the "INT" at the end of "POINT". And the declared type of "STRING" has an affinity of NUMERIC, not TEXT.

    譯者信息

    2.2 親和性名字實例

    下表顯示了有多少從更傳統(tǒng)的SQL實現(xiàn)的常用數(shù)據(jù)類型名,通過上一節(jié)介紹的五個規(guī)則被轉(zhuǎn)換成各種親和性類型。這張表只顯示了SQLite可接受的一小部分數(shù)據(jù)類型名。注意:跟在類型名后,括號內(nèi)數(shù)值參數(shù)(如:VARCHAR(255))將被SQLite忽略 - SQLite不對字符串、BLOBs或數(shù)值的長度強加任何限制(除了大型全局SQLITE_MAX_LENGTH限制)。

    Example Typenames From The
    CREATE TABLE Statement
    or CAST ExpressionResulting AffinityRule Used To Determine Affinity
    INT
    INTEGER
    TINYINT
    SMALLINT
    MEDIUMINT
    BIGINT
    UNSIGNED BIG INT
    INT2
    INT8
    INTEGER1
    CHARACTER(20)
    VARCHAR(255)
    VARYING CHARACTER(255)
    NCHAR(55)
    NATIVE CHARACTER(70)
    NVARCHAR(100)
    TEXT
    CLOB
    TEXT2
    BLOB
    no datatype specified
    NONE3
    REAL
    DOUBLE
    DOUBLE PRECISION
    FLOAT
    REAL4
    NUMERIC
    DECIMAL(10,5)
    BOOLEAN
    DATE
    DATETIME
    NUMERIC5

    注意:?因為在“POINT”末尾的“INT”,一個“?FLOATING POINT”聲明類型?會被賦予整型親和性,而不是實數(shù)親和性。而且“STRING”聲明類型具有數(shù)值親和性,而不是文本親和性。

    ?

    2.3 Column Affinity Behavior Example

    The following SQL demonstrates how SQLite uses column affinity to do type conversions when values are inserted into a table.

    CREATE TABLE t1(t TEXT, -- text affinity by rule 2nu NUMERIC, -- numeric affinity by rule 5i INTEGER, -- integer affinity by rule 1r REAL, -- real affinity by rule 4no BLOB -- no affinity by rule 3 );-- Values stored as TEXT, INTEGER, INTEGER, REAL, TEXT. INSERT INTO t1 VALUES('500.0', '500.0', '500.0', '500.0', '500.0'); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|text-- Values stored as TEXT, INTEGER, INTEGER, REAL, REAL. DELETE FROM t1; INSERT INTO t1 VALUES(500.0, 500.0, 500.0, 500.0, 500.0); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|real-- Values stored as TEXT, INTEGER, INTEGER, REAL, INTEGER. DELETE FROM t1; INSERT INTO t1 VALUES(500, 500, 500, 500, 500); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|integer-- BLOBs are always stored as BLOBs regardless of column affinity. DELETE FROM t1; INSERT INTO t1 VALUES(x'0500', x'0500', x'0500', x'0500', x'0500'); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; blob|blob|blob|blob|blob-- NULLs are also unaffected by affinity DELETE FROM t1; INSERT INTO t1 VALUES(NULL,NULL,NULL,NULL,NULL); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; null|null|null|null|null

    ?

    ?

    譯者信息

    2.3 列親和性行為實例

    以下SQL演示當有值插入到一張表時,SQLite如何使用列親和性實現(xiàn)類型轉(zhuǎn)換的:

    CREATE TABLE t1(t TEXT, -- text affinity by rule 2nu NUMERIC, -- numeric affinity by rule 5i INTEGER, -- integer affinity by rule 1r REAL, -- real affinity by rule 4no BLOB -- no affinity by rule 3 );-- Values stored as TEXT, INTEGER, INTEGER, REAL, TEXT.(值分別以文本、整型、整型、實數(shù)、文本形式存儲) INSERT INTO t1 VALUES('500.0', '500.0', '500.0', '500.0', '500.0'); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|text-- Values stored as TEXT, INTEGER, INTEGER, REAL, REAL. DELETE FROM t1; INSERT INTO t1 VALUES(500.0, 500.0, 500.0, 500.0, 500.0); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|real-- Values stored as TEXT, INTEGER, INTEGER, REAL, INTEGER. DELETE FROM t1; INSERT INTO t1 VALUES(500, 500, 500, 500, 500); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; text|integer|integer|real|integer-- BLOBs are always stored as BLOBs regardless of column affinity. DELETE FROM t1; INSERT INTO t1 VALUES(x'0500', x'0500', x'0500', x'0500', x'0500'); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; blob|blob|blob|blob|blob-- NULLs are also unaffected by affinity DELETE FROM t1; INSERT INTO t1 VALUES(NULL,NULL,NULL,NULL,NULL); SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1; null|null|null|null|null

    3.0 Comparison Expressions

    SQLite version 3 has the usual set of SQL comparison operators including "=", "==", "<", "<=", ">", ">=", "!=", "<>", "IN", "NOT IN", "BETWEEN", "IS", and "IS NOT", .

    3.1 Sort Order

    The results of a comparison depend on the storage classes of the operands, according to the following rules:

    • A value with storage class NULL is considered less than any other value (including another value with storage class NULL).

    • An INTEGER or REAL value is less than any TEXT or BLOB value. When an INTEGER or REAL is compared to another INTEGER or REAL, a numerical comparison is performed.

    • A TEXT value is less than a BLOB value. When two TEXT values are compared an appropriate collating sequence is used to determine the result.

    • When two BLOB values are compared, the result is determined using memcmp().

    譯者信息

    3.0 比較表達式

    同標準SQL一樣,SQLite 3支持如下的比較操作符:"=", "==", "<", "<=", ">", ">=", "!=", "<>", "IN", "NOT IN", "BETWEEN", "IS", 以及 "IS NOT"。

    3.1 排序規(guī)則

    比較的結(jié)果與操作數(shù)的存儲類型有關(guān),同時依據(jù)以下的規(guī)則:

    • NULL值小于其他任何值(包括另外一個NULL)
    • INTEGER或REAL小于TEXT,BLOB值;若兩個INTEGER(或者REAL)比較,則按照實際的數(shù)值進行。
    • TEXT小于BLOB,若兩個TEXT比較,結(jié)果則由適當?shù)恼眄樞驔Q定
    • 若兩個BLOD比較,與memcmp()的結(jié)果一致

    轉(zhuǎn)自:http://www.oschina.net/translate/data-types-in-sqlite-version-3?cmp

    總結(jié)

    以上是生活随笔為你收集整理的SQLite3的数据类型转载()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。