日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
SQLite在Android中使用注意事項(xiàng)

1、模糊查詢的陷阱

十載的米林網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整米林建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“米林網(wǎng)站設(shè)計(jì)”,“米林網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

 
 
  1. cursor = db.rawQuery("select * from song where song_title like '?%' ", selectionArgs); 

這行代碼中由于占位符 ? 在單引號(hào)內(nèi),因此不會(huì)被當(dāng)做占位符,而是對(duì)?進(jìn)行了模糊查找,會(huì)產(chǎn)生類似如下報(bào)錯(cuò):

android.database.SQLite.SQLiteException: bind or column index out of range: handle 0x3418b0

解決方法:

 
 
  1. cursor = db.rawQuery("select * from song where song_title like '" + selectionArgs[0] + "%'", selectionArgs); 

2、cursor.getString(0)方法的陷阱

 
 
  1. cursor = db.rawQuery("select song_singer from song group by song_singer having count(*)<2 ", null); 2 cursor.moveToFirst(); 3 for ( int i= 0; i

以上代碼可以正確實(shí)現(xiàn)從在database中返回的cursor中讀取數(shù)據(jù),但以下代碼會(huì)出現(xiàn)問(wèn)題

 
 
  1. cursor = db.rawQuery("select * from song where song_title like '" + selectionArgs[0] + "%'", null); 2 System.out.println(cursor.getString(0)); 

會(huì)出現(xiàn)類似這個(gè)錯(cuò)誤:android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

解決方法:

 
 
  1. cursor = db.rawQuery("select * from song where song_title like '" + selectionArgs[0] + "%'", null); 2 cursor.moveToFirst(); 3 System.out.println(cursor.getString(0)); 

關(guān)鍵就是這句 cursor.moveToFirst();  

當(dāng)然使用 cursor.getString(0); 方法之后cursor并不會(huì)moveToNext

而對(duì)于SimpleCursorAdapter而言,則不需先進(jìn)行cursor.moveToFirst();  

3、SimpleCursorAdapter的 _id 陷阱

使用SimpleCursorAdapter封裝Cursor時(shí)要求底層數(shù)據(jù)表的主鍵列的列名為_(kāi)id,因?yàn)镾impleCursorAdapter只能識(shí)別列名為_(kāi)id的主鍵

以下代碼會(huì)報(bào)錯(cuò)  java.lang.IllegalArgumentException: column ‘_id’ does not exist

 
 
  1. cursor = db.rawQuery("select song_singer from song where song_singer like '"+selectionArgs[0]+"%' group by song_singer", null); 2 SimpleCursorAdapter simple_adapter = new SimpleCursorAdapter( 3 MusicLookup.this , R.layout.music_lookup_singer_item, cursor 4 , new String[]{"song_singer"} 5 , new int[]{R.id.song_singer_lookup_singer}); 

解決方法:

 
 
  1. cursor = db.rawQuery("select * from song where song_singer like '"+selectionArgs[0]+"%' group by song_singer", null); 2 SimpleCursorAdapter simple_adapter = new SimpleCursorAdapter( 3 MusicLookup.this , R.layout.music_lookup_singer_item, cursor 4 , new String[]{"song_singer"} 5 , new int[]{R.id.song_singer_lookup_singer}); 

要使用SimpleCursorAdapter,則不要在SQL語(yǔ)句中進(jìn)行column的選擇,而是在 new SimpleCursorAdapter(...) 的時(shí)候進(jìn)行對(duì)需要的column的選擇

4、關(guān)于 AutoCompleteTextView 與 SQLite 關(guān)聯(lián)數(shù)據(jù)源的陷阱

AutoCompleteTextView的使用需要ArrayAdapter 適配器來(lái)提供數(shù)據(jù)源,一般都使用 new ArrayAdapter 從字符串的對(duì)象數(shù)組中得到數(shù)據(jù)構(gòu)成ArrayAdapter,對(duì)于靜態(tài)的字符串對(duì)象數(shù)組來(lái)說(shuō),這只需初始化時(shí)直接寫(xiě)入數(shù)據(jù)就行,類似這樣:

 
 
  1. private String[] test = {"a","ab","abc"}; 

這樣便不會(huì)引起 “元素?cái)?shù)<數(shù)組長(zhǎng)度” 的問(wèn)題,然而如果像下面這樣:

 
 
  1. private String[] test = new String[100]; 2 ...... 3 test[0] = "a"; 4 test[1] = "ab"; 5 test[2] = "abc"; 6 ...... 

這就會(huì)引起 “元素?cái)?shù)<數(shù)組長(zhǎng)度” 的問(wèn)題,雖然不會(huì)報(bào)錯(cuò),但使用

ArrayAdapter array_ge_ming = new ArrayAdapter(MusicLookup.this, android.R.layout.simple_dropdown_item_1line, test);

來(lái)初始化ArrayAdapter,并把ArrayAdapter和AutoCompleteTextView關(guān)聯(lián)后,你會(huì)發(fā)現(xiàn),你輸入時(shí)并不會(huì)有自動(dòng)匹配。

從SQLite得來(lái)的數(shù)據(jù)是動(dòng)態(tài)的,是不能對(duì)字符串對(duì)象數(shù)組進(jìn)行事先的靜態(tài)初始化的,為了解決這個(gè)問(wèn)題,我使用了一下方法:

 
 
  1. private String[] str_ge_ming_auto; //聲明時(shí)先不初始化   ......  2 try{  3   cursor = db.rawQuery("select song_title from song", null);  4   cursor.moveToFirst();  5   System.out.println("cursor.getCount() is "+cursor.getCount());  6   str_ge_ming_auto = new String[cursor.getCount()];   //利用從SQLite返回的Cursor對(duì)象的getCount()方法得到需要的數(shù)組長(zhǎng)度  7   for ( int i= 0; i
  2.          + "song_title varchar(20)," + "song_singer varchar(10)," + "song_info varchar(20));"); 18   } 

當(dāng)前標(biāo)題:SQLite在Android中使用注意事項(xiàng)
地址分享:http://www.dlmjj.cn/article/dhihghc.html