确定 SQLite 中列的数据类型
我正在开发一个 Android 应用程序,用户可以使用不同的选项来对来自数据库的显示数据进行排序。目前,我传递给 Android 的 query() 方法的 orderBy 字符串如下所示:
"LOWER("+columnName+") ASC"
问题是,如果 columnName 指定的列中的数据类型是整数,则对其调用 LOWER() 将导致它按字母顺序排序,即仅基于最左边的数字,这当然对于数字数据没有任何意义。因此,如果列的数据类型不是整数,我只想应用 LOWER() 。我的想法是这样的:
"CASE WHEN [data type of columnName is integer] THEN "+columnName+" ASC ELSE LOWER("+columName+") ASC END"
括号里的部分是我不知道该怎么做。 SQLite 是否提供函数来确定列的数据类型?
I'm working on an Android App where the user has different options for sorting the displayed data that comes from the database. Currently my orderBy string that I pass to Androids query() method looks like this:
"LOWER("+columnName+") ASC"
The problem with this is that if the data type in the column specified by columnName is integer, calling LOWER() on it will cause it to be sorted alphabetically, i.e. based only on the leftmost digit, which of course doesn't make any sense for numeric data. Hence I only want to apply LOWER() if the data type of the column is not integer. What I have in mind is a statement like this:
"CASE WHEN [data type of columnName is integer] THEN "+columnName+" ASC ELSE LOWER("+columName+") ASC END"
The part in the brackets is what I don't know how to do. Does SQLite provide a function to determine a column's data type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您确实想要列的类型,还是值的类型? (SQLite 是动态类型的,因此区别很重要。)
如果您想要后者,您可以可以使用
typeof(columnName)
。Do you really want the type of the column, or the type of the value? (SQLite is dynamically-typed, so the distinction is important.)
If you want the latter, you can use
typeof(columnName)
.用途:
获取表信息。
Use:
to get table info.
直接取自有关 SQLite 版本 3 的数据类型的 SQLite 文档 :
列亲和性:使用
PRAGMA table_info(table-name);
。PRAGMA table_info()
给出一个包含列的表格cid
、名称
、类型
、notnull
、dflt_value
和pk
>。值的数据类型:使用
typeof(column)
查看SQLite实际如何存储值。改编自第 3.4 节的示例:
PRAGMA table_info(t1);
的输出:
SELECT 的输出 typeof(t), typeof(nu), typeof(i), typeof(r), typeof( no) FROM t1;
(注意列中的每个值都有自己的数据类型):Taken directly from SQLite docs about datatypes for SQLite Version 3:
Column affinity: use
PRAGMA table_info(table-name);
.PRAGMA table_info()
gives a table with columnscid
,name
,type
,notnull
,dflt_value
, andpk
.Datatype of value: Use
typeof(column)
to see how values are actually stored by SQLite.Example adapted from section 3.4:
Output of
PRAGMA table_info(t1);
:Output of
SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;
(notice each value in a column has its own datatype):您在设置表时是否将列声明为整数?否则,sqlite 会将其存储为文本,并且排序将按照您所描述的方式进行。
Did you declare the column as an integer when setting up the table? Otherwise sqlite will store it as text and the sorts will act as you've described.
获取表使用的信息
如果你想要后者,你可以使用
To get information of Table use
If you want the latter, you can use
使用
typeof(columnName)
您可能并不总是得到相同的结果。我注意到在 blob 上运行测试时,PRAGMA_TABLE_INFO
给我'BLOB'
但typeof
返回加密的'string'.
You may not always get the same result back using
typeof(columnName)
. I noticed that when running a test on a blob,PRAGMA_TABLE_INFO
gives me'BLOB'
buttypeof
returns encrypted'string'
.