温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中SQLite如何使用

发布时间:2021-06-26 15:41:37 来源:亿速云 阅读:159 作者:Leah 栏目:移动开发

本篇文章给大家分享的是有关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<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中读取数据,但以下代码会出现问题

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 &lsquo;_id&rsquo; 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<String>  从字符串的对象数组中得到数据构成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<String> array_ge_ming = new  ArrayAdapter<String>(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<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 }

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI