Android中SQLite的陷阱

1、模糊查询的陷阱

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

解决方法:

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();

以上代码可以正确实现从在database中返回的cursor中读取数据,但以下代码会出现问题

1 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

解决方法:

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

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、关于 AutoCompleteTextView 与 SQLite 关联数据源的陷阱

AutoCompleteTextView的使用需要ArrayAdapter适配器来提供数据源,一般都使用 new ArrayAdapter<String> 从字符串的对象数组中得到数据构成ArrayAdapter,对于静态的字符串对象数组来说,这只需初始化时直接写入数据就行,类似这样:

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

这样便不会引起 “元素数<数组长度” 的问题,然而如果像下面这样:

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

这就会引起 “元素数<数组长度” 的问题,虽然不会报错,但使用

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

来初始化ArrayAdapter,并把ArrayAdapter和AutoCompleteTextView关联后,你会发现,你输入时并不会有自动匹配。

从SQLite得来的数据是动态的,是不能对字符串对象数组进行事先的静态初始化的,为了解决这个问题,我使用了一下方法:

 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()方法得到需要的数组长度 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),"
         + "song_title varchar(20)," + "song_singer varchar(10)," + "song_info varchar(20));"); 18   }

 

 

posted @ 2012-05-31 15:40  maliqian  阅读(9885)  评论(0编辑  收藏  举报