从游标获取数据
Getting Data from Cursor
我需要做的是获取某个用户在某个商店的积分,我运行这个;
Cursor cursor1 = myDB.checkPointss(email, name);
String one = cursor1.getString(cursor1.getColumnIndex("POINTS"));
scanText.setText("barcode resueeeelt " + one);
这就是方法;
public Cursor checkPointss(String email, String name) {
Cursor c = db.rawQuery("SELECT * FROM compdata WHERE EMAIL= ? AND NAME=?", new String[] {email, name});
if (c != null) {
c.moveToFirst();
}
return c;
}
每次我尝试登录页面时,它都会因为试图获得积分而崩溃?我尝试了很多不同的代码,但 none 似乎有效。
所以我只希望它找到包含该用户名和商店名称的行的积分,然后将一定数量的积分添加到积分中并更新数据库。
02-18 20:07:42.694 15570-15570/com.adventorious.quickrewards E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.adventorious.quickrewards, PID: 15570
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:439)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.adventorious.quickrewards.Scan.onPreviewFrame(Scan.java:155)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1016)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
这行得通;
public boolean checkPoints(String email, String name){
Cursor c = db.rawQuery("SELECT POINTS FROM compdata WHERE EMAIL= ? AND NAME=?", new String[] {email, name});
if(c.moveToFirst()) {
return true;
} else {
return false;
}
}
那为什么获取不到数据呢?
getColumnIndex()
区分大小写,而 SQL 不区分大小写。
如果列在 table 中是小写的,它在 SELECT *
结果集中也是小写的,尝试用大写的列名检索它是行不通的。
我需要做的是获取某个用户在某个商店的积分,我运行这个;
Cursor cursor1 = myDB.checkPointss(email, name);
String one = cursor1.getString(cursor1.getColumnIndex("POINTS"));
scanText.setText("barcode resueeeelt " + one);
这就是方法;
public Cursor checkPointss(String email, String name) {
Cursor c = db.rawQuery("SELECT * FROM compdata WHERE EMAIL= ? AND NAME=?", new String[] {email, name});
if (c != null) {
c.moveToFirst();
}
return c;
}
每次我尝试登录页面时,它都会因为试图获得积分而崩溃?我尝试了很多不同的代码,但 none 似乎有效。
所以我只希望它找到包含该用户名和商店名称的行的积分,然后将一定数量的积分添加到积分中并更新数据库。
02-18 20:07:42.694 15570-15570/com.adventorious.quickrewards E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.adventorious.quickrewards, PID: 15570
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:439)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.adventorious.quickrewards.Scan.onPreviewFrame(Scan.java:155)
at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1016)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
这行得通;
public boolean checkPoints(String email, String name){
Cursor c = db.rawQuery("SELECT POINTS FROM compdata WHERE EMAIL= ? AND NAME=?", new String[] {email, name});
if(c.moveToFirst()) {
return true;
} else {
return false;
}
}
那为什么获取不到数据呢?
getColumnIndex()
区分大小写,而 SQL 不区分大小写。
如果列在 table 中是小写的,它在 SELECT *
结果集中也是小写的,尝试用大写的列名检索它是行不通的。