Android sqlite cursor的遍歷實(shí)例詳解
查詢并獲得了cursor對象后,用while(corsor.moveToNext()){}遍歷,當(dāng)corsor.moveToNext()方法調(diào)用,如果發(fā)現(xiàn)沒有對象,會返回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的時候,涉及到sqlite的知識,所以自己想搞一個Demo學(xué)習(xí)一下,看了相關(guān)的教程和幫助文檔,然后開始動手寫自己的程序
//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)時在使用cursor的時候忘了定位cursor,因?yàn)樵诓樵冎蠓祷氐氖且粋€結(jié)果集,也就是一張二維表,如果我們直接調(diào)用getString(int ColumnIndex)的話,就會報錯,因?yàn)楣鈽?biāo)不能夠確定你要返回哪一行的數(shù)據(jù),從而我們在使用Cursor的時候,注意定位光標(biāo)。
以上就是Android sqlite cursor的遍歷實(shí)例詳解的詳細(xì)內(nèi)容,更多關(guān)于Android sqlite cursor的遍歷的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
android?Service基礎(chǔ)(啟動服務(wù)與綁定服務(wù))
大家好,本篇文章主要講的是android?Service基礎(chǔ)(啟動服務(wù)與綁定服務(wù)),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Android開發(fā)高級組件之自動完成文本框(AutoCompleteTextView)用法示例【附源碼下載】
這篇文章主要介紹了Android開發(fā)高級組件之自動完成文本框(AutoCompleteTextView)用法,簡單描述了自動完成文本框的功能并結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)自動完成文本框功能的具體步驟與相關(guān)操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2018-01-01RecycleView實(shí)現(xiàn)item側(cè)滑刪除與拖拽
這篇文章主要為大家詳細(xì)介紹了RecycleView實(shí)現(xiàn)item側(cè)滑刪除與拖拽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11詳解Android首選項(xiàng)框架的使用實(shí)例
首選項(xiàng)這個名詞對于熟悉Android的朋友們一定不會感到陌生,它經(jīng)常用來設(shè)置軟件的運(yùn)行參數(shù)。本篇文章主要介紹詳解Android首選項(xiàng)框架的使用實(shí)例,有興趣的可以了解一下。2016-11-11Android實(shí)現(xiàn)底部圖片選擇Dialog
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部圖片選擇Dialog,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android開發(fā)實(shí)現(xiàn)圖片圓角的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)圖片圓角的方法,涉及Android針對圖形圖像的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10Android之禁止ViewPager滑動實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android之禁止ViewPager滑動實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05