2

我是这些 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");
  }            }
4

1 回答 1

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();
          }    
于 2013-09-19T10:41:16.277 回答