android - sqlite中cursor的问题
天蓬老师
天蓬老师 2017-04-17 16:02:11
0
0
309

我建立了一个表public static final String CREATE_BOOK = "create table Book(" + "id integer primary key autoincrement," + "author text," + "price real," + "pages integer," + "name text," + "category_id integer)";
之后建立了一个Button传入数据

add_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = dbhelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("author", "jose"); values.put("name", "Thank you!"); values.put("pages", 999); values.put("price", 99.9); db.insert("Book", null, values); } });

之后建立另一个按钮用来索引表

query_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = dbhelper.getWritableDatabase(); Cursor cursor = db.query("Book", null, null, null, null, null, null); if(cursor.moveToFirst()){ do { String name = cursor.getString(cursor.getColumnIndex("name")); String author = cursor.getString(cursor.getColumnIndex("author")); int pages = cursor.getInt(cursor.getColumnIndex("pages")); double price = cursor.getDouble(cursor.getColumnIndex("price")); Log.d("MainActivity", "book name is" + name); Log.d("MainActivity", "book author is" + author); Log.d("MainActivity", "book pages is" + pages); Log.d("MainActivity", "book prices is" + price); }while (cursor.moveToNext()); } cursor.close(); } });

但是调试时报错Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 5 columns. Make sure the Cursor is initialized correctly before accessing data from it.
我将传入数据时的name和author的顺序交换之后发现就可以运行了,请教一下各位大神这是什么原因?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

répondre à tous (0)
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!