1、模糊查詢的陷阱

cursor = db.rawQuery("select * from song where song_title like '?%' ", selectionArgs);
這行代碼中由於預留位置 ? 在單引號內,因此不會被當做預留位置,而是對?進行了模糊查找,會產生類似如下報錯:
android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x3418b0
解決方法:
cursor = db.rawQuery("select * from song where song_title like '" + selectionArgs[0] + "%'", selectionArgs);
2、cursor.getString(0)方法的陷阱
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
以上代碼可以正確實現從在database中返回的cursor中讀取資料,但以下代碼會出現問題
cursor = db.rawQuery("select * from song where song_title like '" + selectionArgs[0] + "%'", null); 2 System.out.println(cursor.getString(0));
會出現類似這個錯誤:android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
解決方法:
cursor = db.rawQuery("select * from song where song_title like '" + selectionArgs[0] + "%'", null); 2 cursor.moveToFirst(); 3 System.out.println(cursor.getString(0));
關鍵就是這句 cursor.moveToFirst();
當然使用 cursor.getString(0); 方法之後cursor並不會moveToNext
而對於SimpleCursorAdapter而言,則不需先進行cursor.moveToFirst();
3、SimpleCursorAdapter的 _id 陷阱
使用SimpleCursorAdapter封裝Cursor時要求底層資料表的主鍵列的列名為_id,因為SimpleCursorAdapter只能識別列名為_id的主鍵
以下代碼會報錯 java.lang.IllegalArgumentException: column ‘_id’ does not exist
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});
解決方法:
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語句中進行column的選擇,而是在 new SimpleCursorAdapter(...) 的時候進行對需要的column的選擇
4、關於 AutoCompleteTextView 與 SQLite 關聯資料來源的陷阱
AutoCompleteTextView的使用需要ArrayAdapter 配接器來提供資料來源,一般都使用 new ArrayAdapter 從字串的物件陣列中得到資料構成ArrayAdapter,對於靜態的字串物件陣列來說,這只需初始化時直接寫入資料就行,類似這樣:
private String[] test = {"a","ab","abc"};
這樣便不會引起 「元素數<陣列長度」 的問題,然而如果像下面這樣:
private String[] test = new String[100]; 2 ...... 3 test[0] = "a"; 4 test[1] = "ab"; 5 test[2] = "abc"; 6 ......
這就會引起 「元素數<陣列長度」 的問題,雖然不會報錯,但使用
ArrayAdapter array_ge_ming = new ArrayAdapter(MusicLookup.this, android.R.layout.simple_dropdown_item_1line, test);
來初始化ArrayAdapter,並把ArrayAdapter和AutoCompleteTextView關聯後,你會發現,你輸入時並不會有自動匹配。
從SQLite得來的資料是動態的,是不能對字串物件陣列進行事先的靜態初始化的,為了解決這個問題,我使用了一下方法:
private String[] str_ge_ming_auto; 聲明時先不初始化 ......
try{ 3 cursor = db.rawQuery("select song_title from song", null);
cursor.moveToFirst();
System.out.println("cursor.getCount() is "+cursor.getCount());
str_ge_ming_auto = new String[cursor.getCount()]; 利用從SQLite返回的Cursor物件的getCount()方法得到需要的陣列長度
for ( int i= 0; i
{
str_ge_ming_auto[i] = cursor.getString(0);
System.out.println("str_ge_ming_auto[i] is "+str_ge_ming_auto[i]); 一個個賦值
cursor.moveToNext();
}
cursor.close();
System.out.println("str_ge_shou_auto finish");
}catch(SQLiteException se){
db.execSQL("create table song(_id integer primary key autoincrement," + "song_num Varchar(5),"
+ "song_title Varchar(20)," + "song_singer Varchar(10)," + "song_info Varchar(20));");
}
 
來源:
http://www.cnblogs.com/maliqian/archive/2012/05/31/2527161.html
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()