Get the App
SLTechnology News&Howtos  ›  Database  › 

How to realize fuzzy query by using SQL statement

Shulou Source: shulou.com Published: 2022-05-31 19:33:59 11月08日 Update

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.

Tags: Data query statement content just database method analysis up and down context professional small and medium key keyword rich in content character string instance object that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple Shulou Information macOS Shulou Technology