简体   繁体   English

如何从 cursor 中获取类型?

[英]How do you get the type from a cursor?

The javadocs indicate that android.database.Cursor has a getType(int) method. javadocs 表明 android.database.Cursor 有一个 getType(int) 方法。 However, when I try to call this method, Eclipse gives me an error saying no method exists.但是,当我尝试调用此方法时,Eclipse 给我一个错误,提示不存在方法。 What gives?是什么赋予了?

What version of Android are you targeting?您的目标是什么版本的 Android? The getType() method applies only to v3.0 and later. getType()方法仅适用于 v3.0 及更高版本。

EDIT: Assuming you're targeting pre v3.0 versions of Android then a possible 'hack' to discover the type of each column within a table would be to query the sqlite_master table to find the CREATE data.编辑:假设您的目标是 Android 的 v3.0 之前的版本,那么发现表中每一列的类型的可能“黑客”方法是查询 sqlite_master 表以查找 CREATE 数据。

SELECT sql FROM sqlite_master

It's pretty nasty but if you really need to find the type then you could extend SQLiteCursor and add your own getType(int columnIndex) method.这很讨厌,但如果你真的需要找到类型,那么你可以扩展 SQLiteCursor 并添加你自己的getType(int columnIndex)方法。

You could perform a one-off query to the sqlite_master table when the cursor first accesses a table then parse the CREATE statement from the sql column and store the 'types' in an int[] .您可以在 cursor 第一次访问表时对sqlite_master表执行一次性查询,然后解析sql列中的CREATE语句并将“类型”存储在int[]中。

In the getType(int columnIndex) you would simply return the value of your int[] based on columnindex .getType(int columnIndex)中,您只需根据columnindex返回int[]的值。

As I said, bit of a hack but it would work.正如我所说,有点破解,但它会工作。

As you can store any type of data in (nearly) every SQLite field.因为您可以在(几乎)每个 SQLite 字段中存储任何类型的数据。 Looking up the data type in sqlite_master and by using the pragma command is not a safe method.在 sqlite_master 中查找数据类型并使用 pragma 命令不是一种安全的方法。 You can get the type of the fields using a query like this:您可以使用如下查询获取字段的类型:

select Field1, typeof(Field1), Field2, typeof(Field2) from MyTable

This statement returns both the value of the fields and their types which might / might not change from record to record.此语句返回字段的值及其类型,它们可能/可能不会因记录而异。

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

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