SQLite在Android中使用注意事项

移动开发 Android
SQLite在Android中使用有一些陷阱,比如模糊查询的陷阱、cursor.getString(0)方法的陷阱、SimpleCursorAdapter的 _id 陷阱和关于 AutoCompleteTextView 与 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),"  
  2.          + "song_title varchar(20)," + "song_singer varchar(10)," + "song_info varchar(20));"); 18   } 
责任编辑:徐川 来源: OSChina
相关推荐

2009-07-24 13:40:16

使用SilverLig

2021-08-26 14:55:55

开发React代码

2009-07-01 02:29:24

临时表T-SQL

2014-07-01 12:49:06

Android Stu安装

2010-02-05 14:13:17

Android平台

2015-07-29 14:20:30

微信支付注意事项

2011-07-19 10:16:58

喷墨打印机注意事项

2010-01-18 14:25:19

使用C++Builde

2010-11-26 16:27:01

MySQL使用变量

2012-03-02 10:51:06

JavaJDBC

2010-01-21 11:30:10

2009-12-22 09:48:58

WCF枚举类型

2010-09-16 09:52:49

CSS display

2012-03-12 16:46:22

NoSQL数据库

2011-07-28 17:29:22

HBaseShell

2011-04-14 11:28:07

光纤

2009-12-15 17:47:17

VSIP

2011-05-26 11:22:04

SEO

2011-08-01 12:53:25

iPhone 多线程 线程

2009-07-16 11:40:23

ibatis自动生成abator
点赞
收藏

51CTO技术栈公众号