快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Android中SQLite如何使用

本篇文章给大家分享的是有关Android中SQLite如何使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

创新互联是一家集网站建设,乐昌企业网站建设,乐昌品牌网站建设,网站定制,乐昌网站建设报价,网络营销,网络优化,乐昌网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

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; //声明时先不初始化 ......  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

以上就是Android中SQLite如何使用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


文章标题:Android中SQLite如何使用
分享地址:http://6mz.cn/article/pidiid.html

其他资讯