android sqlite使用之模糊查詢數據庫數據的三種方式

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



本文來自http://www.blogjava.net/qileilove/archive/2013/10/11/404852.html


我測試過方法3,可行,代碼如下

Cursor cursor = db.getReadableDatabase().rawQuery("select * from S where Sno LIKE '%" + Ban +"____'",null);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章