我有以下代码。我从db查询中获取了一个游标对象,该查询实际上返回一个整数值。我需要从游标对象获取整数值。我正在尝试以下方法,它返回一个异常。任何人都可以告诉我如何从光标中获取整数值。查询是正确的我使用一些数据库工具尝试了相同的查询,我得到了所需的值。请帮我解决这个问题:
我的数据库查询:
public int getTaskUID(String taskName) {
Cursor cursor = db.rawQuery("SELECT _id FROM DataTable WHERE Task_name = ' " +taskName + "'" , null);
int count = Integer.parseInt(String.valueOf(cursor)) ;
Log.d("Pana", "the value of count is " + count);
return count;
}
我的adb追踪:
--------- beginning of crash
03-12 11:23:29.392 1129-1129/com.ms.t.tms E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ms.t.tms, PID: 1129
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NumberFormatException: Invalid int: "android.database.sqlite.SQLiteCursor@59327fe"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:410)
at java.lang.Integer.parseInt(Integer.java:367)
at java.lang.Integer.parseInt(Integer.java:334)
at com.ms.t.tms.SQLiteDataBaseAdapter.getTaskUID(SQLiteDataBaseAdapter.java:382)
at com.ms.t.tms.InsertSubTaskActivity.addUser(InsertSubTaskActivity.java:502)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我的数据库架构:
public static final String CREATE_TABLE = " CREATE TABLE " + TABLE_NAME +
"(" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TASK_NAME + " VARCHAR(250)," + CONTACT_NAME + " VARCHAR(250)," + CONTACT_NUMBER + " VARCHAR(250),"
+ CONTACT_EMAIL + " VARCHAR(250)," + DESCRIPTION + " VARCHAR(250), " + REMARKS + " VARCHAR(250),"
+ DATE + " VARCHAR(250)," + TIME + " VARCHAR(250)," + ESTIMATED_COMPLETION_DATE + " VARCHAR(250), " + ESTIMATED_COMPLETION_TIME + " VARCHAR(250), "
+ ACTUAL_COMPLETION_DATE + " VARCHAR(250), " + ACTUAL_COMPLETION_TIME + " VARCHAR(250), " + NOTIFY_DATE + " VARCHAR(250), " + NOTIFY_TIME + " VARCHAR(250), " + SUB_TASK_NUMBER + " VARCHAR(250) );";
如何将游标对象转换为int值。请帮忙。
答案 0 :(得分:2)
你可以从光标获得价值,如
int count = Integer.parseInt(cursor.getString(0)) ;
int count = cursor.getInt(cursor.getColumnIndex("db_column_name"));
答案 1 :(得分:1)
您无法将游标值转换为整数作为结果持有者对象。您可以尝试从游标中提取值并将其解析为整数。
类似的东西:
int count = cursor.getCount();//to get number of rows your query gave
或者,如果您想要请求结果,那么:
int count = cursor.getInt(cursor.getColumnIndex("_id"));
答案 2 :(得分:1)
你不能这样做:
int count = Integer.parseInt(String.valueOf(cursor)) ;
Log.d("Pana", "the value of count is " + count);
在您的代码中执行以下操作:
Cursor cursor = db.rawQuery("SELECT _id FROM DataTable WHERE Task_name = ' " +taskName + "'" , null);
if(cursor != null && cursor.getCount > 0) {
cursor.moveToFirst()
}
long id = cursor.getLong(cursor.getColumnIndex("_id"));
使用以下代码从光标
获取值 // Reading Long Value
long id = cursor.getLong(cursor.getColumnIndex("_id"));
//Reading String value
String name = cursor.getString(cursor.getColumnIndex("table_column_name"));
//Reading Double value
String name = cursor.getDouble(cursor.getColumnIndex("table_column_name"));
//Reading Integer value
String name = cursor.getInt(cursor.getColumnIndex("table_column_name"));
计算游标中的记录数
int count = (cursor != null && cursor.getCount() > 0) ? cursor.getCount() : 0;