如何将数据库连接到Android的SEARCH按钮

时间:2012-05-16 02:40:38

标签: android sqlite

如何单击SEARCH按钮,从我的SQLite数据库中检索数据,并将其显示在我的应用程序中?

我的活动:

public class EKaDEMActivity extends Activity implements OnClickListener {

    //EditText ET;
    public static final String KEY_ROWID = "Dic_id";
    public static final String colID="_id";
    public static final String colKD="KD_word";
    public static final String colENG="ENG_word";
    public static final String colMAL="Malay_word";
    public static final String colAudio="KD_audio";

    private static final String dbName="Dictionary";
    private static final String dbTable="tbl_Dictionary";
    private static final int dbVersion = 2;

    static final String viewEmps="ViewEmps";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageButton button = (ImageButton)findViewById(R.id.button_one);
        button.setOnClickListener(this);

    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub  


        EditText ET = (EditText) findViewById(R.id.AutoTextBox);
        ET = (EditText) findViewById(R.id.AutoTextBox);

        String s = ET.getText().toString();
        TextView TV = (TextView)findViewById(R.id.DefTextView);
        TV.setText("Welcome Using Kadazandusun-English-Malay Mobile Dictionary Application");
        boolean r = s.contentEquals("dog");
        if (r == true)
        {
            String str = "  M: Anging \n\n" + "  KD: Tasu ";
            TextView text = (TextView)findViewById(R.id.DefTextView);
            text.setText(str);
        }
        else
        {
            TV.setText("   Word not found");

        }               
    }


}

这是我的Database.java

public static final String KEY_ROWID = "Dic_id";
public static final String colID="_id";
public static final String colKD="KD_word";
public static final String colENG="ENG_word";
public static final String colMAL="Malay_word";
public static final String colAudio="KD_audio";

private static final String dbName="Dictionary";
private static final String dbTable="tbl_Dictionary";
private static final int dbVersion = 1;

static final String viewEmps="ViewEmps";

public Database(Context context, String name, CursorFactory factory,
        int version) {
    super(context, dbName, null, dbVersion);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL("CREATE TABLE " + dbTable + " (" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            colKD + " TEXT NOT NULL, " +
            colENG + " TEXT NOT NULL, " +
            colMAL + " TEXT NOT NULL, );");

    db.execSQL("CREATE VIEW "+viewEmps+
            " AS SELECT "+dbTable+"."+colID+" AS _id,"+
            " "+dbTable+"."+colKD+","+
            " "+dbTable+"."+colENG+","+
            " "+dbTable+"."+colMAL+""+
            " FROM "+dbTable
            );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS "+ dbTable);
    onCreate(db);
}

 void AddWord(Word wrd)
 {


     SQLiteDatabase db= this.getWritableDatabase();


    ContentValues cv=new ContentValues();

    cv.put(colKD, wrd.getKD());
    cv.put(colENG, wrd.getENG());
    cv.put(colMAL, wrd.getMAL());
    //cv.put(colDept,2);

    db.insert(dbTable, colKD, cv);
    db.close();


}

 public int UpdateWrd(Word wrd)
 {
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put(colKD, wrd.getKD());
    cv.put(colENG, wrd.getENG());
    cv.put(colMAL, wrd.getMAL());
    return db.update(dbTable, cv, colID+"=?", new String []{String.valueOf(wrd.getID())});

 }

 public void DeleteWrd(Word wrd)
 {
     SQLiteDatabase db=this.getWritableDatabase();
     db.delete(dbTable,colID+"=?", new String [] {String.valueOf(wrd.getID())});
     db.close();

 }

Cursor getWord()
 {
     SQLiteDatabase db=this.getWritableDatabase();

     Cursor cur= db.rawQuery("SELECT * FROM "+viewEmps,null);
     return cur;
 }

}

2 个答案:

答案 0 :(得分:0)

你应该阅读更多关于SQLite和Android的教程,你了解其中的一些,但你可以学到更多。

您需要在EKaDEMActivity中创建一个Database对象,可能是这样的:

Database database;
...

onCreate() {
    ...
    database = new Database(this);
}

替换它:

boolean r = s.contentEquals("dog");
if (r == true)

有类似的东西:

Cursor cursor = database.getWord(s);
if(cursor.moveToNext()) {
    ...
    text.setText(cursor.getString(cursor.getColumnIndex(colKD)));

在getWords()中:

Cursor getWord(String word) {
     SQLiteDatabase db=this.getWritableDatabase();

     return db.rawQuery("SELECT * FROM "+viewEmps + " WHERE " + colENG + "=?", new String[] { word });
}

这只搜索英文单词并显示KD列中的匹配单词,您必须根据需要进行更改。了解每次搜索单词时获取新的可写数据库的效率非常低,您应该只调用一次getWritableDatabase()并将db保存在类变量中。此外,您应该在完成后关闭游标或将其传递给活动,以便它自动关闭它们。祝你好运!

答案 1 :(得分:0)

将这两个函数添加到数据库类中。

public Cursor searchall()throws Exception// For searching all record 
{
    return db.rawQuery("Select * from info ORDER BY regno ",null);
}

public Cursor search(String regno)throws Exception // for searching record with where clause
{
    return db.rawQuery("Select * from info where regno='"+regno+"'",null);
}

将此代码添加到“搜索”按钮

btnreg.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) 
{
    try 
    {
    String num = regno.getText().toString();
        dba.open();
        cursor = dba.search(num);
        if (cursor.moveToFirst()) 
        {
            do 
            {
                regno.setText(cursor.getString(0));
                name.setText(cursor.getString(1));
                fname.setText(cursor.getString(2));
                age.setText(cursor.getString(3));
                city.setText(cursor.getString(4));
            }
            while (cursor.moveToNext());
            dba.close();
        }
    }
}