简体   繁体   English

带有整数列的 SQLite 表存储字符串

[英]SQLite table with integer column stores string

I found something weired in my application.我在我的应用程序中发现了一些奇怪的东西。 I created a table with a column named type which should store integers:我创建了一个名为type的列的表,该列应存储整数:

db.execSQL("CREATE TABLE " + CellColumns.TABLE + " ("
    + CellColumns._ID + " INTEGER PRIMARY KEY,"
    + CellColumns.TYPE + " INTEGER," // <-- this
    + CellColumns.CELL_ID + " INTEGER,"
    + CellColumns.CITY_ID + " INTEGER,"
    + CellColumns.LOAD + " INTEGER,"
    + CellColumns.ORIENTATION + " INTEGER);");

Reading:读:

String type = c.getString(c.getColumnIndex(CellColumns.TYPE));

But somehow I always store strings into it without any problems (seems I forgot that this column was meant for integers).但不知何故,我总是将字符串存储到其中而没有任何问题(似乎我忘记了该列是用于整数的)。 Also reading the strings with a query works.还可以通过查询读取字符串。 Is this dynamic "type cast" of a column a feature of sqlite?列的这种动态“类型转换”是 sqlite 的一个特性吗?

From here : 这里

Most SQL database engines (every SQL database engine other than SQLite, as far as we know) uses static, rigid typing. 大多数SQL数据库引擎(除了SQLite之外的每个SQL数据库引擎,据我们所知)都使用静态,严格的类型。 With static typing, the datatype of a value is determined by its container - the particular column in which the value is stored. 使用静态类型时,值的数据类型由其容器(存储值的特定列)确定。

SQLite uses a more general dynamic type system. SQLite使用更通用的动态类型系统。 In SQLite, the datatype of a value is associated with the value itself, not with its container. 在SQLite中,值的数据类型与值本身相关联,而不是与其容器相关联。 The dynamic type system of SQLite is backwards compatible with the more common static type systems of other database engines in the sense that SQL statement that work on statically typed databases should work the same way in SQLite. SQLite的动态类型系统向后兼容其他数据库引擎的更常见的静态类型系统,因为在静态类型数据库上工作的SQL语句应该在SQLite中以相同的方式工作。 However, the dynamic typing in SQLite allows it to do things which are not possible in traditional rigidly typed databases. 但是,SQLite中的动态类型允许它执行传统的刚性类型数据库中无法实现的操作。

So yes, this is a feature of sqlite. 所以是的,这是sqlite的一个特性。 Read the whole page I linked, and read carefully :) 阅读我链接的整个页面,仔细阅读:)

你可以通过c.getInt()c.getDouble() ..等获得它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM