qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

android sqlite使用之模糊查询数据库数据的三种方式

 android应用开发中常常需要记录一下数据,而在查询的时候如何实现模糊查询呢?很少有文章来做这样的介绍,所以这里简单的介绍下三种sqlite的模糊查询方式,直接上代码把:
package com.example.utils;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBManage extends SQLiteOpenHelper {
static int init_version = 1;
static String database_name = "android_sqlite_test.db";
static String tab_name = "uer_log";
static String tab_field01 = "_id";
static String tab_field02 = "log_name";
SQLiteDatabase mDatabase;
public DBManage(Context context) {
super(context, database_name, null, init_version);
// TODO Auto-generated constructor stub
mDatabase = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql = "create table " + tab_name + " ( " + tab_field01
+ " integer primary key , " + tab_field02 + " text  not null) ";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/**
* 插入记录
*
* @param u
* @return
*/
public boolean insertData(String... str) {
int request_int = 0;
for (int i = 0; i < str.length; i++) {
// 实例化一个ContentValues 对象 ,作用,收集数据,方便于SQLite执行增,删,改,查
ContentValues contentValues = new ContentValues();
contentValues.put(tab_field02, str[i]);
mDatabase.insert(tab_name, null, contentValues);
request_int++;
}
return str.length == request_int;
}
// 根据条件模糊查询数据库数据
public ArrayList<String> query(int top_int, String... str) {
ArrayList<String> result_list = new ArrayList<String>();
mDatabase = getReadableDatabase();
//模糊查询的三种方式:
/*
* 全部查询
String current_sql_sel = "SELECT  * FROM " + tab_name;
Cursor c = mDatabase.rawQuery(current_sql_sel, null);*/
//1.使用这种query方法%号前不能加' ;
Cursor c_test = mDatabase.query(tab_name, new String[]{tab_field02}, tab_field02+"  LIKE ? ",
new String[] { "%" + str[0] + "%" }, null, null, null);
//2.使用这种query方法%号前必须加'  ;
//  Cursor  c_test=mDatabase.query(tab_name, new String[]{tab_field02},tab_field02+"  like '%" + str[0] + "%'", null, null, null, null);
//3.使用这种方式必须在%号前加'  ;
String current_sql_sel = "SELECT  * FROM "+tab_name +" where "+tab_field02+" like '%"+str[0]+"%'";
//Cursor c_test = mDatabase.rawQuery(current_sql_sel, null);
Log.e("tag", "查询完成...");
while (c_test.moveToNext()) {
String name = c_test.getString(c_test.getColumnIndex(tab_field02));
//name.contains(str[0]);
// 让集合中的数据不重复;
if (!result_list.contains(name)) {
result_list.add(name);
Log.e("tag", name);
}
}
c_test.close();
return result_list;
}
}

 SQL模糊查询语句
  SQL模糊查询,使用like比较字,加上SQL里的通配符,请参考以下:
  1、LIKE'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden)。
  2、LIKE'%inger' 将搜索以字母 inger 结尾的所有字符串(如 Ringer、Stringer)。
  3、LIKE'%en%' 将搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。
  4、LIKE'_heryl' 将搜索以字母 heryl 结尾的所有六个字母的名称(如 Cheryl、Sheryl)。
  5、LIKE'[CK]ars[eo]n' 将搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
  6、LIKE'[M-Z]inger' 将搜索以字符串 inger 结尾、以从 M 到 Z 的任何单个字母开头的所有名称(如 Ringer)。
  7、LIKE'M[^c]%' 将搜索以字母 M 开头,并且第二个字母不是 c 的所有名称(如MacFeather)。
  下面这句查询字符串,根据变量 zipcode_key 在邮政编码表 zipcode 中查询对应的数据,这句是判断变量 zipcode_key 为非数字时的查询语句,用 % 来匹配任意长度的字符串,从表中地址、市、省三列中查询包含关键字的所有数据项,并按省、市、地址排序。这个例子比较简单,只要你理解了方法就可以写出更复杂的查询语句。
  sql = "select * from zipcode where (address like'%" & zipcode_key & "%') or (city like'%" & zipcode_key & "%') or (province like'%" & zipcode_key & "%') order by province,city,address

posted on 2013-10-11 09:56 顺其自然EVO 阅读(15713) 评论(1)  编辑  收藏 所属分类: android

评论

# f[未登录] 2015-10-15 15:22 f

ff  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年10月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜