Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize fuzzy query by using SQL statement

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to use SQL sentences to achieve fuzzy query. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

In main.xml:

In the MyDatabaseHelper.java class:

Package com.li.sqlite

/ / Auxiliary operation class of database

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; / / set the version of the database

Private static final String TABLENAME = "mytab"

Public MyDatabaseHelper (Context context) {/ / users are definitely only concerned about Context

Super (context, DATABASENAME, null, DATABASERVERSION)

}

@ Override

Public void onCreate (SQLiteDatabase db) {/ / create datasheet

String sql = "CREATE TABLE" + TABLENAME + "("

+ "id INTEGER PRIMARY KEY," / / set to Integer in SQLite, PRIMARY KEY will automatically increase ID

+ "name VARCHAR (50) NOT NULL,"

+ "birthday DATE NOT NULL" + ")"

Db.execSQL (sql); / / execute SQL

System.out.println ("* create: onCreate ().")

}

@ Override

Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {

String sql = "DROP TABLE IF EXISTS" + TABLENAME

Db.execSQL (sql)

System.out.println ("* Update: onUpgrade ().")

This.onCreate (db)

}

}

In the MytabCursor.java class:

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 find () {

List all = new ArrayList (); / / it's just String at this time

String sql = "SELECT id,name,birthday FROM" + TABLENAME + "WHERE name LIKE? OR birthday LIKE?"

String keyWord = "2"; / / query keyword, which should be defined by the method

String args [] = new String [] {"%" + keyWord + "%", "%" + keyWord + "%"}

Cursor result = this.db.rawQuery (sql, args); / / execute query statement

For (result.moveToFirst ();! result.isAfterLast (); result.moveToNext ()) {/ / retrieve data in a circular manner

All.add ("[" + result.getInt (0) + "]" + "" + result.getString (1)

+ "," + result.getString (2)

}

This.db.close ()

Return all

}

}

In 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 (/ / set data

New ArrayAdapter / / all data is a string

(MySQLiteDemo.this, / / context

Android.R.layout.simple_list_item_1, / / layout displayed in the list

New MytabCursor (/ / instantiate query

MySQLiteDemo.this.helper.getReadableDatabase () / / get the SQLiteDatabase object

.find ()); / / call the find () method and return List

MySQLiteDemo.this.mylayout.addView (listView)

}

}

}

The above is the editor for you to share how to use SQL statements to achieve fuzzy query, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report