【问题标题】:how to retrieve single row according to specific condition?如何根据特定条件检索单行?
【发布时间】:2015-12-11 04:30:53
【问题描述】:

这里是我的编码:- 数据库助手.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);
        }
}
}

基本上我想根据 id="f1" 显示 id,name,drink,price。 例如:如果 id="f1" 则显示所有信息而不使用任何搜索或单击按钮。

当我运行此编码时,应用程序已停止

【问题讨论】:

    标签: android


    【解决方案1】:

    您需要将选择参数作为数组传递,但在选择字符串中您应该放置? 标记。像这样更改您的 getinnformationUser 方法:

    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;
    }
    

    您可以阅读更多关于查询方法here的信息。

    【讨论】:

      【解决方案2】:

      基本上你的选择查询是错误的......如下所示

      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;
      }
      

      会完美运行...

      【讨论】:

      【解决方案3】:

      使用 orderBy。在 orderBy 中传递任何值以对行进行排序,例如,如果您想按 id 排序,则传递 id 并添加 limit 1

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

      这将只选择一行。

      更正 您也可以使用query()(Eric B. 提供的链接)作为参数传递限制

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-01
        • 1970-01-01
        • 2023-03-05
        • 2012-12-08
        • 2019-08-25
        • 1970-01-01
        • 1970-01-01
        • 2012-06-10
        相关资源
        最近更新 更多