蔡作馨列的数据类型在一个SQLite的Android游标 [英] Indentifying datatype of a column in an SQLite Android Cursor
问题描述
有没有什么办法,以确定光标在Android中一列的数据类型。光标对象有许多方法来获得的ColumnName,列值
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.
我要找出列(TEXT,INTEGER)等的SQLite的数据类型...
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://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()
(见<一href="http://developer.android.com/reference/android/database/AbstractWindowedCursor.html#getType(int" rel="nofollow">http://developer.android.com/reference/android/database/AbstractWindowedCursor.html#getType(int)).
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屋!