0

如何只需单击“搜索”按钮,从我的 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;
 }

}

4

2 回答 2

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();
        }
    }
}
于 2012-05-16T04:47:49.200 回答
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 保存在类变量中。此外,您应该在完成后关闭光标或将它们传递给 Activity,以便它会自动为您关闭它们。祝你好运!

于 2012-05-16T03:50:35.270 回答