-1

here my coding:- DatabaseHelper.java

public Cursor getinnformationUser(SQLiteDatabase db) {
    String a1="f1";
    Cursor cursor;
    String[] projections = {Menu.NewMenuInfo.MENU_ID, Menu.NewMenuInfo.MENU_NAME, Menu.NewMenuInfo.MENU_PRICE};
    String selection = Menu.NewMenuInfo.MENU_ID + "=" + a1;
    cursor=db.query(Menu.NewMenuInfo.TABLE_NAME,projections,selection,null,null,null,null);
    return cursor;
}

Menu1.java

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menuset1);
    s_id = (TextView) findViewById(R.id.idSet1);
    s_name = (TextView) findViewById(R.id.nameSet1);
    s_drink = (TextView) findViewById(R.id.drinkSet1);
    s_price = (TextView) findViewById(R.id.priceSet1);
    set1 = (Button) findViewById(R.id.buttonOrderSet);
    userDbHelper = new UserDbHelper(getApplicationContext());
    sqLiteDatabase = userDbHelper.getReadableDatabase();
    cursor = userDbHelper.getinnformationSet1(sqLiteDatabase);
    if (cursor != null){
        cursor.moveToFirst();
            String id = cursor.getString(0);
            String name = cursor.getString(1);
            String drink = cursor.getString(2);
            String price = cursor.getString(3);
            s_id.setText(id);
            s_name.setText(name);
            s_drink.setText(drink);
            s_price.setText(price);
        }
}
}

basically i want to display id,name,drink,price according to id="f1". example: if id="f1" then display all the information without using any searching or click button.

when i run this coding the application has stopped

hasni
  • 29
  • 8

3 Answers3

0

You need to pass the selection arguments as an array, but in the selection string you should put ? mark. Change your getinnformationUser method like this:

public Cursor getinnformationUser(SQLiteDatabase db) {
    String a1="f1";
    Cursor cursor;
    String[] projections = {Menu.NewMenuInfo.MENU_ID, Menu.NewMenuInfo.MENU_NAME, Menu.NewMenuInfo.MENU_PRICE};
    String selection = Menu.NewMenuInfo.MENU_ID + "= ?";
    String[] selectionArgs = new String[] {a1};
    cursor=db.query(Menu.NewMenuInfo.TABLE_NAME,projections,selection,selectionArgs,null,null,null,null);
    return cursor;
}

You can read more about the query method here.

Eric B.
  • 4,622
  • 2
  • 18
  • 33
0

basically your select query is wrong....its like below

public Cursor getinnformationUser(SQLiteDatabase db) {
    String a1="f1";
    Cursor cursor;
    String[] projections = {Menu.NewMenuInfo.MENU_ID, Menu.NewMenuInfo.MENU_NAME, Menu.NewMenuInfo.MENU_PRICE};
    String whereclause = Menu.NewMenuInfo.MENU_ID + "= ?";
    String where_args[] = {a1};

    cursor=db.query(Menu.NewMenuInfo.TABLE_NAME,projections,whereclause,where_args,null,null,null);
    return cursor;
}

Will work perfectly...

H Raval
  • 1,903
  • 17
  • 39
0

Use orderBy. In orderBy pass any value for order the rows for example if you want to order by id then pass id and add limit 1 i.e

cursor=db.query(Menu.NewMenuInfo.TABLE_NAME,projections,selection,null,null,null,"id limit 1");

This will select only one row.

Correction You can use query() (link provided by Eric B.) also for passing limit as parameter

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67