如何定义以编程方式从sqlite中检索数据的过程?

时间:2013-09-18 10:39:46

标签: android sqlite

我是这些sqlite访问编程的新手。我有9个edittext并编码为在sqlite db中存储值...下次如果我在第一个edittext中插入相同的值,剩下的值应该显示而不输入..下面的代码是什么我已经尝试但无法取回值

//this is my dbhandler class method

> public Data findProduct(String phone) {       String query = "Select *
> FROM " + TABLE_FEEDBACK + " WHERE " + COLUMN_PHONENO + " =  \"" +
> phone + "\"";
>               SQLiteDatabase db = this.getWritableDatabase();
                Cursor cursor = db.rawQuery(query, null);
          Data data = new Data();
          if (cursor.moveToFirst()) {             cursor.moveToFirst();           data.setPHONE(cursor.getString(0));
      data.setName(cursor.getString(1));
      data.setFood(cursor.getString(2));
      data.setService(cursor.getString(3));
      data.setCleanliness(cursor.getString(4));
      data.setExperience(cursor.getString(5));
      data.setAddress(cursor.getString(6));
      data.setEmail(cursor.getString(7));
      data.setBirthday(cursor.getString(8));
      data.setAnniversary(cursor.getString(9));
      data.setFavrestaurant(cursor.getString(10));
      data.setFavstaff(cursor.getString(11));
      data.setFavdish(cursor.getString(12));
      data.setSuggestion(cursor.getString(13));
      data.setReturn(cursor.getString(14));           cursor.close();         } else {
      data = null;        }
      db.close();         return data;    }
     
/*public void openDataBase(SQLiteDatabase db) throws SQLException {   

    //Open the database       
    String myPath = DB_PATH + DATABASE_NAME;      
    db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);     
    }  */
  
//this is my mainactivity method
     
public void lookupProduct (View view) {
     DBHandler dbHandler = new DBHandler(MainActivity.this);

     Data data = 
      dbHandler.findProduct(phonenumber.getText().toString());

     if (data != null) {
       name.setText(String.valueOf(data.getName()));
       address.setText(String.valueOf(data.getAddress()));
  } else {
         name.setText("No Match Found");
  }            }
  

1 个答案:

答案 0 :(得分:2)

我通过在dbhandler类中定义方法并在主活动中调用它们来实现这一点

public Data findProduct(String phone) {
    String query = "Select * FROM " + TABLE_FEEDBACK + " WHERE " + COLUMN_PHONENO + " =  \"" + phone + "\"";
    System.out.println("this is the product >>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor cursor = db.rawQuery(query, null);

    Data data0 = new Data();

    if (cursor.moveToFirst()) {
        cursor.moveToFirst();
        data0.setPHONE(cursor.getString(0));
        data0.setName(cursor.getString(1));

        data0.setAddress(cursor.getString(6));
        data0.setEmail(cursor.getString(7));


        cursor.close();
    } else {
        data0 = null;
    }
    System.out.println("data set value :>>>>>>>");
        db.close();
    return data0;
}

这是click()

下的主要活动代码
EditText phonenumber = (EditText) findViewById(R.id.editText1);
              String phonenmbr = phonenumber.getText().toString();
              Data data0 = dbHandler.findProduct(phonenmbr);
if (data0 != null) {
                 phonenumber.setText(String.valueOf(data0.getPHONE()));
                 customername.setText(String.valueOf(data0.getName()));
                 customeraddress.setText(String.valueOf(data0.getAddress()));
                 customeremail.setText(String.valueOf(data0.getEmail()));



          } else {
              Toast.makeText(MainActivity.this, "Please enter other details", Toast.LENGTH_SHORT).show();
          }