识别SQLite Android游标中的列的数据类型 [英] Identifying datatype of a column in an SQLite Android Cursor
问题描述
有什么方法可以识别Android中游标中的列的数据类型.游标对象具有许多获取列名和列值的方法.
Is there any way to identify the datatype of a column in a cursor in Android. The cursor object has a number of methods to get the columnname, column value.
我想找出列的SQLite数据类型(TEXT,INTEGER)等...
I want to find out the SQLite datatype of the column (TEXT, INTEGER) etc...
我正在编写一个通用函数来解析游标并执行操作.我只会得到一个sql字符串作为该函数的参数.
I'm writing a generic function to parse a cursor and perform operations. I will only get a sql string as an argument to the function.
推荐答案
根据SQLite文档( http:/SQLite中的/www.sqlite.org/datatype3.html )列没有数据类型,这些列中的值却有.
Per the SQLite documentation (http://www.sqlite.org/datatype3.html) columns in SQLite don't have a datatype -- the values in those columns do.
SQLite版本3数据库中的任何列(除了INTEGER PRIMARY KEY列之外)都可以用于存储任何存储类的值.
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.
如果您使用的是API级别11或更高版本,则光标支持getType()
(请参阅
If you're using API level 11 or above then the cursor supports getType()
(see http://developer.android.com/reference/android/database/AbstractWindowedCursor.html#getType(int)).
如果您使用的是较早的API级别,并且知道给定游标中的所有结果都来自同一张表,则可以执行以下操作(未测试):
If you're using an earlier API level, and you know that all the results in a given cursor come from the same table then you could do something like (untested):
// Assumes "cursor" is a variable that contains the cursor you're
// interested in.
String tableName = "..."; // The name of the table
SQLiteDatabase db = cursor.getDatabase();
String[] names = cursor.getColumnNames();
for (name : names) {
Cursor typeCursor =
db.rawQuery("select typeof (" + name + ") from " + tableName;
typeCursor.moveToFirst();
Log.v("test", "Type of " + name + " is " + typeCursor.getString(0);
}
但是,如果传入的游标是(例如)连接了两个或多个表的db.rawQuery()调用的结果,那(我期望)将失败.
But that will (I expect) fail if the passed in cursor was (for instance) the result of a db.rawQuery() call that joined two or more tables.
这篇关于识别SQLite Android游标中的列的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!