自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SQLite在Android中使用注意事項

移動開發(fā) Android
SQLite在Android中使用有一些陷阱,比如模糊查詢的陷阱、cursor.getString(0)方法的陷阱、SimpleCursorAdapter的 _id 陷阱和關(guān)于 AutoCompleteTextView 與 SQLite 關(guān)聯(lián)數(shù)據(jù)源的陷阱,本文介紹了這些陷阱和相關(guān)的解決方法。

1、模糊查詢的陷阱

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

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

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<cursor.getCount(); i++ ) 4 { 5 str_ge_shou_auto[i] = cursor.getString(0); 6 System.out.println("str_ge_shou_auto[i] is "+str_ge_shou_auto[i]); 7  cursor.moveToNext(); 8 } 9 cursor.close(); 

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

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

會出現(xiàn)類似這個錯誤: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并不會moveToNext

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

3、SimpleCursorAdapter的 _id 陷阱

使用SimpleCursorAdapter封裝Cursor時要求底層數(shù)據(jù)表的主鍵列的列名為_id,因為SimpleCursorAdapter只能識別列名為_id的主鍵

以下代碼會報錯  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語句中進行column的選擇,而是在 new SimpleCursorAdapter(...) 的時候進行對需要的column的選擇

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

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

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

這樣便不會引起 “元素數(shù)<數(shù)組長度” 的問題,然而如果像下面這樣:

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

這就會引起 “元素數(shù)<數(shù)組長度” 的問題,雖然不會報錯,但使用

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

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

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

  1. private String[] str_ge_ming_auto; //聲明時先不初始化   ......  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對象的getCount()方法得到需要的數(shù)組長度  7   for ( int i= 0; i<cursor.getCount(); i++ )  8   {  9   str_ge_ming_auto[i] = cursor.getString(0); 10   System.out.println("str_ge_ming_auto[i] is "+str_ge_ming_auto[i]);  //一個個賦值 11   cursor.moveToNext(); 12   } 13   cursor.close(); 14    15   System.out.println("str_ge_shou_auto finish"); 16   }catch(SQLiteException  se){ 17   db.execSQL("create table song(_id integer primary key autoincrement," + "song_num varchar(5),"  
  2.          + "song_title varchar(20)," + "song_singer varchar(10)," + "song_info varchar(20));"); 18   } 
責(zé)任編輯:徐川 來源: OSChina
相關(guān)推薦

2009-07-24 13:40:16

使用SilverLig

2009-07-01 02:29:24

臨時表T-SQL

2021-08-26 14:55:55

開發(fā)React代碼

2010-02-05 14:13:17

Android平臺

2014-07-01 12:49:06

Android Stu安裝

2010-11-26 16:27:01

MySQL使用變量

2015-07-29 14:20:30

微信支付注意事項

2011-07-19 10:16:58

噴墨打印機注意事項

2010-01-18 14:25:19

使用C++Builde

2011-04-14 11:28:07

光纖

2011-07-28 17:29:22

HBaseShell

2012-03-12 16:46:22

NoSQL數(shù)據(jù)庫

2012-03-02 10:51:06

JavaJDBC

2010-09-16 09:52:49

CSS display

2010-01-21 11:30:10

2009-12-22 09:48:58

WCF枚舉類型

2009-12-15 17:47:17

VSIP

2011-05-26 11:22:04

SEO

2011-07-26 18:43:34

HBase Shell

2009-08-27 10:40:56

Java路徑
點贊
收藏

51CTO技術(shù)棧公眾號