這期內容當中小編將會給大家帶來有關使用SQL語句怎么實現模糊查詢,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:id="@+id/findBut"
android:layout_marginTop="8dp"
android:background="#0066ff"
android:textColor="#ffffff"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="查詢全部數據" />
</LinearLayout>
在MyDatabaseHelper.java類中:
package com.li.sqlite;
//數據庫的輔助操作類
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASENAME = "liyewen.db" ;
private static final int DATABASERVERSION = 1 ; // 設置數據庫的版本
private static final String TABLENAME = "mytab" ;
public MyDatabaseHelper(Context context) { // 用戶最關心的也肯定只是Context
super(context, DATABASENAME, null, DATABASERVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { // 創建數據表
String sql = "CREATE TABLE " + TABLENAME + "("
+ "id INTEGER PRIMARY KEY ," // 在SQLite中設置為Integer、PRIMARY KEY則ID自動增長
+ "name VARCHAR(50) NOT NULL ,"
+ "birthday DATE NOT NULL" + ")";
db.execSQL(sql) ; // 執行SQL
System.out.println("****************** 創建:onCreate()。");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + TABLENAME ;
db.execSQL(sql) ;
System.out.println("****************** 更新:onUpgrade()。");
this.onCreate(db) ;
}
}
在MytabCursor.java類中:
package com.li.sqlite;
import java.util.ArrayList;
import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MytabCursor {
private static final String TABLENAME = "mytab" ;
private SQLiteDatabase db = null ;
public MytabCursor(SQLiteDatabase db) {
this.db = db ;
}
public List<String> find(){
List<String> all = new ArrayList<String>() ; // 此時只是String
String sql = "SELECT id,name,birthday FROM " + TABLENAME + " WHERE name LIKE ? OR birthday LIKE ?" ;
String keyWord = "2" ; // 查詢關鍵字 ,應該由方法定義
String args[] = new String[] { "%" + keyWord + "%", "%" + keyWord + "%" };
Cursor result = this.db.rawQuery(sql, args); // 執行查詢語句
for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) { // 采用循環的方式檢索數據
all.add("【" + result.getInt(0) + "】" + " " + result.getString(1)
+ "," + result.getString(2));
}
this.db.close() ;
return all ;
}
}
在MySQLiteDemo.java中:
package com.li.sqlite;
import android.app.Activity;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
public class MySQLiteDemo extends Activity {
private Button findBut = null;
private SQLiteOpenHelper helper = null;
private LinearLayout mylayout = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.findBut = (Button)super.findViewById(R.id.findBut);
this.mylayout = (LinearLayout)super.findViewById(R.id.mylayout);
this.findBut.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MySQLiteDemo.this.helper = new MyDatabaseHelper(MySQLiteDemo.this);
ListView listView = new ListView(MySQLiteDemo.this);
listView.setAdapter( //設置數據
new ArrayAdapter<String> //所有的數據是字符串
(MySQLiteDemo.this, //上下文
android.R.layout.simple_list_item_1, //列表顯示的布局
new MytabCursor( //實例化查詢
MySQLiteDemo.this.helper.getReadableDatabase()) //取得SQLiteDatabase對象
.find())); //調用find()方法,返回List<String>
MySQLiteDemo.this.mylayout.addView(listView);
}
}
}
上述就是小編為大家分享的使用SQL語句怎么實現模糊查詢了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。