java SQLite 数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3388098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLite data types
提问by Manuel Selva
I am using SQLitein a Java application through Zentus. In this context I need to save and query Java longvalues in my database. Coming from other RDBMS I created the table as following to store long values:
我通过Zentus在 Java 应用程序中使用SQLite。在这种情况下,我需要在我的数据库中保存和查询 Java值。来自其他 RDBMS,我创建了如下表来存储长值:long
CREATE TABLE myTable (id INTEGER PRIMARY_KEY, longValue LONG)
This solution produces the excepted behavior but after reading the SQLite documentation on data typesI understood that my LONGtype has the same effect than using TEXT=> longValueis stored as text.
该解决方案产生了异常行为,但在阅读了有关数据类型的SQLite 文档后,我了解到我的LONG类型与使用TEXT=>longValue存储为文本具有相同的效果。
I then decided to change this to INTEGER(which length is variable and can store up to 64 bit integers which is the length of Java long) in order to have cleaner code and may be to save some disk space and to increase performances because my longValues are inserted and queried as long.
然后我决定将其更改为INTEGER(长度是可变的,并且可以存储最多 64 位整数,这是 Java 的长度)以获得更清晰的代码,并且可能是为了节省一些磁盘空间并提高性能,因为我的 longValues 是插入并查询为long.
After comparing the performances and the size of the created databases I am not able to see any difference between:
在比较了创建的数据库的性能和大小后,我看不出它们之间有什么区别:
CREATE TABLE myTable (id INTEGER PRIMARY_KEY, longValue LONG)
and
和
CREATE TABLE myTable (id INTEGER PRIMARY_KEY, longValue INTEGER)
Any comments, experiences or feelings on the subject?
对这个主题有什么评论、经历或感受吗?
回答by Tangent 128
In SQLite, data types are per-value, not per-column. So when you insert integers, they're stored as integers regardless of the column type.
在 SQLite 中,数据类型是每个值,而不是每个列。因此,当您插入整数时,无论列类型如何,它们都将存储为整数。
回答by Christian Ullenboom
SQLite chooses automatically the right size. From http://www.sqlite.org/datatype3.html:
SQLite 会自动选择正确的大小。来自http://www.sqlite.org/datatype3.html:
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
整数。该值是一个有符号整数,根据值的大小存储在 1、2、3、4、6 或 8 个字节中。
SQLite uses dynamic types and is schema free.
SQLite 使用动态类型并且是无模式的。
回答by dan04
After looking at performances and size of the created databases I am not able to see any difference between:
在查看了创建的数据库的性能和大小后,我看不出它们之间的任何区别:
There isn'tany difference. INTEGER has integer affinity and LONG has numeric affinity. And, http://www.sqlite.org/datatype3.htmlsays:
目前没有任何区别。INTEGER 具有整数关联性,而 LONG 具有数字关联性。而且,http: //www.sqlite.org/datatype3.html说:
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.
使用 INTEGER 关联的列的行为与具有 NUMERIC 关联的列相同。INTEGER 和 NUMERIC 亲和性之间的差异仅在 CAST 表达式中很明显。
回答by Rahul Baradia
CREATE TABLE ex2(
a VARCHAR(10),
b NVARCHAR(15),
c TEXT,
d INTEGER,
e FLOAT,
f BOOLEAN,
g CLOB,
h BLOB,
i TIMESTAMP,
j NUMERIC(10,5)
k VARYING CHARACTER (24),
l NATIONAL VARYING CHARACTER(16)
);

