這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)使用SQL語(yǔ)句怎么實(shí)現(xiàn)模糊查詢,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)公司2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元二道江做網(wǎng)站,已為上家服務(wù),為二道江各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
在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="查詢?nèi)繑?shù)據(jù)" />
</LinearLayout>
在MyDatabaseHelper.java類中:
package com.li.sqlite;
//數(shù)據(jù)庫(kù)的輔助操作類
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 ; // 設(shè)置數(shù)據(jù)庫(kù)的版本
private static final String TABLENAME = "mytab" ;
public MyDatabaseHelper(Context context) { // 用戶最關(guān)心的也肯定只是Context
super(context, DATABASENAME, null, DATABASERVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { // 創(chuàng)建數(shù)據(jù)表
String sql = "CREATE TABLE " + TABLENAME + "("
+ "id INTEGER PRIMARY KEY ," // 在SQLite中設(shè)置為Integer、PRIMARY KEY則ID自動(dòng)增長(zhǎng)
+ "name VARCHAR(50) NOT NULL ,"
+ "birthday DATE NOT NULL" + ")";
db.execSQL(sql) ; // 執(zhí)行SQL
System.out.println("****************** 創(chuàng)建: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>() ; // 此時(shí)只是String
String sql = "SELECT id,name,birthday FROM " + TABLENAME + " WHERE name LIKE ? OR birthday LIKE ?" ;
String keyWord = "2" ; // 查詢關(guān)鍵字 ,應(yīng)該由方法定義
String args[] = new String[] { "%" + keyWord + "%", "%" + keyWord + "%" };
Cursor result = this.db.rawQuery(sql, args); // 執(zhí)行查詢語(yǔ)句
for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) { // 采用循環(huán)的方式檢索數(shù)據(jù)
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( //設(shè)置數(shù)據(jù)
new ArrayAdapter<String> //所有的數(shù)據(jù)是字符串
(MySQLiteDemo.this, //上下文
android.R.layout.simple_list_item_1, //列表顯示的布局
new MytabCursor( //實(shí)例化查詢
MySQLiteDemo.this.helper.getReadableDatabase()) //取得SQLiteDatabase對(duì)象
.find())); //調(diào)用find()方法,返回List<String>
MySQLiteDemo.this.mylayout.addView(listView);
}
}
}
上述就是小編為大家分享的使用SQL語(yǔ)句怎么實(shí)現(xiàn)模糊查詢了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文標(biāo)題:使用SQL語(yǔ)句怎么實(shí)現(xiàn)模糊查詢
URL地址:http://bm7419.com/article14/psdjge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站內(nèi)鏈、網(wǎng)站導(dǎo)航、響應(yīng)式網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)