SQLite表与整数列存储字符串 [英] SQLite table with integer column stores string

查看:182
本文介绍了SQLite表与整数列存储字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中发现了一些东西。我创建了一个带有类型的列的表格,该列应该存储整数:

I found something weired in my application. I created a table with a column named type which should store integers:

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);");

阅读:

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

但不知怎的,我总是把字符串存储到它没有任何问题(似乎我忘了这个列是为整数)。也可以通过查询来读取字符串。这个动态的类型转换列是sqlite的一个特性吗?

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?

推荐答案

here


大多数SQL数据库引擎(除了SQLite之外的每个SQL数据库引擎,据我们所知) ,刚性类型。使用静态类型,值的数据类型由其容器(存储值的特定列)确定。

Most SQL database engines (every SQL database engine other than SQLite, as far as we know) uses static, rigid typing. With static typing, the datatype of a value is determined by its container - the particular column in which the value is stored.

SQLite使用更通用的动态类型系统。在SQLite中,值的数据类型与值本身相关联,而不与其容器相关联。 SQLite的动态类型系统向后兼容其他数据库引擎的更常见的静态类型系统,因为在静态类型数据库上工作的SQL语句应该在SQLite中以相同的方式工作。但是,SQLite中的动态类型允许它执行在传统的刚性类型数据库中不可能做的事情。

SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container. 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. 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. Read the whole page I linked, and read carefully :)

这篇关于SQLite表与整数列存储字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆