简体   繁体   English

如何从android sqlite中的特定单元格检索Real类型数据?

[英]How to retrieve Real type data from specific cell in android sqlite?

在此处输入图片说明

In this table, I want to retrieve the value 55.2 in a variable.在此表中,我想检索变量中的值 55.2。 This value is at row 5 (ID=5) and in the column 'Weight' of type REAL.该值位于第 5 行 (ID=5) 和 REAL 类型的“重量”列中。

I can already get the desired row number which is stored in 'lastID' and I know that my data is in the column 'Weight'.我已经可以获得存储在“lastID”中的所需行号,并且我知道我的数据在“重量”列中。 So I have my X and my Y in the table.所以我在桌子上有我的 X 和我的 Y。

I also know the sqlite command to retrieve the 55.2 in my cursor:我也知道在我的光标中检索 55.2 的 sqlite 命令:

Cursor cursor2 = db.rawQuery("SELECT Weight FROM <MYTABLE> WHERE ID=" + lastID, null);
Double lastWeight = cursor2.getDouble(0);  //This line is wrong, I need the help here!

But I can't get the 55.2 value I am looking for in my variable lastWeight from cursor2.但是我无法从 cursor2 的变量 lastWeight 中获取我正在寻找的 55.2 值。

Any idea?任何的想法?

Addendum附录

Here the create table:这里创建表:

String CREATE_TABLE2 = "CREATE TABLE " + <MYTABLE> + " (" + UID2 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COL_2 + " TEXT," + COL_3 + " TEXT," + COL_4 + " REAL," + COL_5 + " REAL);";
db.execSQL(CREATE_TABLE2);

After the execution of this line:执行此行后:

Cursor cursor2 = db.rawQuery("SELECT Weight FROM <MYTABLE> WHERE ID=" + lastID, null);

you get the results in cursor2 .你在cursor2得到结果。
A Cursor instance like cursor2 is used to loop through its rows and to do so you must first place its index at the 1st row by moveToFirst() :cursor2这样的Cursor实例用于循环遍历其行,为此您必须首先通过moveToFirst()将其索引放在第一行:

if (cursor2.moveToFirst()) {
    Double lastWeight = cursor2.getDouble(0);
    ........................................
}

The if statement is necessary just in case the cursor does not contain any rows. if语句是必要的,以防游标不包含任何行。

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

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