在SQLite数据库上检索数据时出错

时间:2016-02-16 12:44:19

标签: android sqlite

我在检索数据时遇到问题,它说make sure cursor is initialized correctly before accessing data from it我无法弄清楚光标初始化的问题,

我的logcat下面。

      02-16 20:34:49.678 22333-22333/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.computer.mathkiddofinal:second, PID: 22333
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.computer.mathkiddofinal/com.example.computer.mathkiddofinal.Database.Record_DB}: 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.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                       at android.os.Handler.dispatchMessage(Handler.java:110)
                                                       at android.os.Looper.loop(Looper.java:193)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                       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:828)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
                                                       at dalvik.system.NativeStart.main(Native Method)
                                                    Caused by: 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.nativeGetLong(Native Method)
                                                       at android.database.CursorWindow.getLong(CursorWindow.java:507)
                                                       at android.database.CursorWindow.getInt(CursorWindow.java:574)
                                                       at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:80)
                                                       at com.example.computer.mathkiddofinal.Database.Record_DB.onCreate(Record_DB.java:55)
                                                       at android.app.Activity.performCreate(Activity.java:5264)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                       at android.os.Looper.loop(Looper.java:193) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                       at java.lang.reflect.Method.invokeNative(Native Method) 

Record_DB.java,此行int postwh = res.getInt(res.getColumnIndex("POSTWH"));

中的错误
public class Record_DB extends Activity {
Final_Result_Grade_4 asd;
TextView txtDB;
    DatabaseHelper myDb;
    ArrayList<String> results = new ArrayList<String>();
    ArrayList<Integer> lpostwh = new ArrayList<Integer>();
    ArrayList<Integer> lpostmul = new ArrayList<Integer>();
    ArrayList<Integer> lpostdiv = new ArrayList<Integer>();
    ArrayList<Integer> lprewh = new ArrayList<Integer>();
    ArrayList<Integer> lpremul = new ArrayList<Integer>();
    ArrayList<Integer> lprediv = new ArrayList<Integer>();
    ListView errorListview;
    ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record__db);
        errorListview = (ListView) findViewById(R.id.listtt);
        //TextView txtDB = (TextView) findViewById(R.id.txtDB);
       // txtDB.setMovementMethod(new ScrollingMovementMethod());
        myDb = new DatabaseHelper(this);
        Cursor res = myDb.getAllData();
        int cols = res.getColumnCount();
        if (res.getCount() == 0) {
            Toast.makeText(Record_DB.this, "no data", Toast.LENGTH_SHORT).show();
            return;
        }

        while (res.moveToNext()) {
            int id = res.getInt(res.getColumnIndex("ID"));
            String name = res.getString(res.getColumnIndex("NAME"));
            int pre = res.getInt(res.getColumnIndex("PRETEST"));
            int post = res.getInt(res.getColumnIndex("POSTTEST"));
            int postwh = res.getInt(res.getColumnIndex("POSTWH"));
            int postmul = res.getInt(res.getColumnIndex("POSTMUL"));
            int postdiv = res.getInt(res.getColumnIndex("POSTDIV"));
            int prewh = res.getInt(res.getColumnIndex("PREWH"));
            int premul = res.getInt(res.getColumnIndex("PREMUL"));
            int prediv = res.getInt(res.getColumnIndex("PREDIV"));
            results.add(""+postwh + postmul + postdiv + prewh + premul + prediv );


        }

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results);
        errorListview.setAdapter(adapter);

我无法弄清楚如何解决这个问题,任何人都可以提供帮助。非常感谢谢谢

DataBaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "gradefour.db";
    public static final String TABLE_NAME = "grade_four_table";
    public static final String COL_1= "ID";
    public static final String COL_2= "NAME";
    public static final String COL_3= "PRETEST";
    public static final String COL_4= "POSTTEST";
    public static final String COL_5= "POSTWH";
    public static final String COL_6= "POSTMUL";
    public static final String COL_7= "POSTDIV";
    public static final String COL_8= "PREWH";
    public static final String COL_9= "PREMUL";
    public static final String COL_10= "PREDIV";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PRETEST INTEGER,POSTTEST INTEGER,POSTWH INTEGER,POSTMUL INTEGER,POSTDIV INTEGER,PREWH INTEGER,PREMUL INTEGER,PREDIV INTEGER) ");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    }
    public boolean inserData(String name, Integer pretest, Integer posttest, Integer postwh, Integer postmul, Integer postdiv,
                             Integer prewh, Integer premul, Integer prediv){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,pretest);
        contentValues.put(COL_4,posttest);
        contentValues.put(COL_5,postwh);
        contentValues.put(COL_6,postmul);
        contentValues.put(COL_7,postdiv);
        contentValues.put(COL_8,prewh);
        contentValues.put(COL_9,premul);
        contentValues.put(COL_10,prediv);
       long result =  db.insert(TABLE_NAME,null,contentValues);
       if(result==-1){
           return false;
       }
        else
       {
           return true;
       }
    }
    public Cursor getAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);

        return res;
    }
}

3 个答案:

答案 0 :(得分:1)

在阅读光标前

res.moveToFirst();

答案 1 :(得分:1)

根据你的评论.. !!

您在安装应用后添加了大约6列。因此,您必须需要在创建新表后重新安装应用,并在表格中添加6列。

只有在安装了应用程序并且当时创建了表时才调用OnCreate()方法,因此当您需要进行一些更改时,您有两个选项

  1. 在OnUpgrade()方法中进行更改并更新您的应用。 (适用于实时应用)

  2. 卸载应用并再次安装。 (做测试)

答案 2 :(得分:1)

尝试迭代光标,如下例所示,不要忘记关闭数据库以及光标对象。

if(cursor.moveToFirst()){
   ...
}
db.close();
cursor.close();