国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Android sqlite cursor的遍歷實(shí)例詳解

瀏覽:125日期:2022-09-17 18:27:42

查詢并獲得了cursor對象后,用while(corsor.moveToNext()){}遍歷,當(dāng)corsor.moveToNext()方法調(diào)用,如果發(fā)現(xiàn)沒有對象,會(huì)返回false

public List<MMImage> getAll() {List<MMImage> list = new ArrayList<MMImage>();Cursor c = null;try { c = database.query(TABLE, null, null, null, null, null, null); while (c.moveToNext()) {MMImage mmImage = getMMImageFromCursor(c);list.add(mmImage); }} catch (Exception e) { e.printStackTrace();} finally { if (c != null) {c.close(); }}return list; }

知識點(diǎn)內(nèi)容擴(kuò)展:

寫android的時(shí)候,涉及到sqlite的知識,所以自己想搞一個(gè)Demo學(xué)習(xí)一下,看了相關(guān)的教程和幫助文檔,然后開始動(dòng)手寫自己的程序

//1.獲取SQLiteDatabase的對象SQliteDataBase sqlite = SQliteDatabase.openOrCreateDatabase(new File(Environment.getExternalStorageDirectory() + 'testDB'),null);//2.向數(shù)據(jù)庫中存入數(shù)據(jù)sqlite.execSQL('create table student(id varchar2(10),name varchar2(20),sex varchar2(2)');sqlite.execSQL('insert into student values(?,?,?)', new String[] {'2013111111', 'Tom', 'M' });//3.從sqlite中讀取數(shù)據(jù)Cursor cursor = sqlite.rawQuery('select * from student', null);//輸出列名for (int i = 0; i < cursor.getColumnCount(); i++) { textView.append(cursor.getColumnName(i) + ’t’);}textView.append('n');//開始讀取其中的數(shù)據(jù)if (cursor.moveToFirst()) { do {textView.append(cursor.getString(0) + ’t’ + cursor.getString(1) + ’t’ + cursor.getString(2) + ’n’); } while (cursor.moveToNext());}

看起了很簡單,但是我當(dāng)時(shí)在使用cursor的時(shí)候忘了定位cursor,因?yàn)樵诓樵冎蠓祷氐氖且粋€(gè)結(jié)果集,也就是一張二維表,如果我們直接調(diào)用getString(int ColumnIndex)的話,就會(huì)報(bào)錯(cuò),因?yàn)楣鈽?biāo)不能夠確定你要返回哪一行的數(shù)據(jù),從而我們在使用Cursor的時(shí)候,注意定位光標(biāo)。

以上就是Android sqlite cursor的遍歷實(shí)例詳解的詳細(xì)內(nèi)容,更多關(guān)于Android sqlite cursor的遍歷的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 普兰县| 西峡县| 台前县| 利辛县| 汶川县| 铜梁县| 彭水| 新郑市| 巍山| 上高县| 西青区| 慈利县| 习水县| 贵州省| 达拉特旗| 宁夏| 阿鲁科尔沁旗| 衢州市| 镇巴县| 合江县| 遂宁市| 岗巴县| 佛学| 西宁市| 康平县| 巴彦县| 高唐县| 射阳县| 郑州市| 湾仔区| 航空| 沂源县| 乌兰察布市| 乌兰浩特市| 措勤县| 巩留县| 碌曲县| 贵南县| 榆树市| 漯河市| 滁州市|