我正在使用 sqlite db 编写可搜索的字典,我的助手类代码如下:
package com.riffre.dictionary.sharma;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_WR = "word";
public static final String KEY_DF = "defn";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "w";
private static final String DATABASE_TABLE = "defns";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table defns (_id integer primary key autoincrement, "
+ "word text not null, defn text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS ");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long insertTitle(String word, String defn)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_WR, word);
initialValues.put(KEY_DF, defn);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
//---retrieves all the titles---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_WR,
KEY_DF,},
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_WR,
KEY_DF,
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a title---
public boolean updateTitle(long rowId, String word,
String defn, String publisher)
{
ContentValues args = new ContentValues();
args.put(KEY_WR, word);
args.put(KEY_DF, defn);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
}
我正在通过以下类在数据库中手动添加单词:
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Search extends Activity {
EditText qr;
Button search;
DBAdapter db ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
db = new DBAdapter(this);
db.open();
long id;
id = db.insertTitle("ASSIGNAT", " One of the notes, bills, or bonds, issued as currency by the revolutionary government of France (1790-1796), and based on the security of the lands of the church and of nobles which had been appropriated by the state.");
id = db.insertTitle("ASSIGNATION", "1. The act of assigning or allotting; apportionment.This order being taken in the senate, as touching the appointment and assignation of those provinces. Holland. 2. An appointment of time and place for meeting or interview; -- used chiefly of love interviews, and now commonly in a bad sense.While nymphs take treats, or assignations give. Pope.3. A making over by transfer of title; assignment. House of assignation, a house in which appointments for sexual intercourse are fulfilled.");
id = db.insertTitle("ASSIGNEE", "In England, the persons appointed, under a commission of bankruptcy, to manage the estate of a bankrupt for the benefit of his creditors.");
id = db.insertTitle("ASSIGNER", "One who assigns, appoints, allots, or apportions.");
id = db.insertTitle("ASSIMILABILITY", "The quality of being assimilable. [R.] Coleridge.");
id = db.insertTitle("ASSIMILABLE", "That may be assimilated; that may be likened, or appropriated and incorporated.");
id = db.insertTitle("ASSIMILATE", "To become similar or like something else.");
id = db.insertTitle("ASSIMULATE", "To feign; to counterfeit; to simulate; to resemble.");
id = db.insertTitle("ASSIMULATION", "Assimilation.");
id = db.insertTitle("ASSINEGO", "See Asinego.");
qr=(EditText)findViewById(R.id.srchtxt);
search=(Button)findViewById(R.id.srchbtn);
search.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String qu=qr.getText().toString();
Log.v("s",qu);
}
});
}
}
那么,如果我想通过数据库中的单词进行搜索,我该如何完成呢?提前致谢