sqlite使用模糊查询数据库数据的三种方式
android應(yīng)用開發(fā)中常常需要記錄一下數(shù)據(jù),而在查詢的時候如何實現(xiàn)模糊查詢呢?很少有文章來做這樣的介紹,所以這里簡單的介紹下三種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 對象 ,作用,收集數(shù)據(jù),方便于SQLite執(zhí)行增,刪,改,查 ContentValues contentValues = new ContentValues(); contentValues.put(tab_field02, str[i]); mDatabase.insert(tab_name, null, contentValues); request_int++; } return str.length == request_int; } // 根據(jù)條件模糊查詢數(shù)據(jù)庫數(shù)據(jù) 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]); // 讓集合中的數(shù)據(jù)不重復(fù); 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 結(jié)尾的所有字符串(如 Ringer、Stringer)。
3、LIKE'%en%' 將搜索在任何位置包含字母 en 的所有字符串(如 Bennet、Green、McBadden)。
4、LIKE'_heryl' 將搜索以字母 heryl 結(jié)尾的所有六個字母的名稱(如 Cheryl、Sheryl)。
5、LIKE'[CK]ars[eo]n' 將搜索下列字符串:Carsen、Karsen、Carson 和 Karson(如 Carson)。
6、LIKE'[M-Z]inger' 將搜索以字符串 inger 結(jié)尾、以從 M 到 Z 的任何單個字母開頭的所有名稱(如 Ringer)。
7、LIKE'M[^c]%' 將搜索以字母 M 開頭,并且第二個字母不是 c 的所有名稱(如MacFeather)。
下面這句查詢字符串,根據(jù)變量 zipcode_key 在郵政編碼表 zipcode 中查詢對應(yīng)的數(shù)據(jù),這句是判斷變量 zipcode_key 為非數(shù)字時的查詢語句,用 % 來匹配任意長度的字符串,從表中地址、市、省三列中查詢包含關(guān)鍵字的所有數(shù)據(jù)項,并按省、市、地址排序。這個例子比較簡單,只要你理解了方法就可以寫出更復(fù)雜的查詢語句。
sql = "select * from zipcode where (address like'%" & zipcode_key & "%') or (city like'%" & zipcode_key & "%') or (province like'%" & zipcode_key & "%') order by province,city,address
總結(jié)
以上是生活随笔為你收集整理的sqlite使用模糊查询数据库数据的三种方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超方便、最简单版本:java 邮件发送
- 下一篇: springcloud 注解 @Enab