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

我建立了一个表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的顺序交换之后发现就可以运行了,请教一下各位大神这是什么原因?

天蓬老师
天蓬老师

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

全部回覆 (0)
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!