【问题标题】:Checking that conditions are met before adding the order to the database在将订单添加到数据库之前检查是否满足条件
【发布时间】:2020-10-11 14:32:15
【问题描述】:

我试图确保用户选择了一个服务员 ID(第一个 if 语句)并且该 ID 是有效的并且与数据库中的一条记录相关(The else if 语句)。

我正在从Number (GUI Text in Android) 中收集id,并像这样选择Number

 EditText waiter_id;
 waiter_id = (EditText) findViewById(R.id.waiter_id);

TextUtils.isEmpty(waiter_id.getText() 检查以确保输入了一个值,如果没有,则显示一条消息。

在调用DataBaseHelper isEmployee(String id) 类中创建的方法后,该方法使用用户输入的id 在数据库中搜索该记录。如果在数据库中未找到该 ID,则应显示一条消息以提醒用户。

Order onClick OrderActivity

order.setOnClickListener(new View.OnClickListener() {
            Order order;
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(waiter_id.getText())) {
                    Toast.makeText(OrderActivity.this, "Please select waiter id", Toast.LENGTH_SHORT).show();

                // This is where the method which returns the boolean is called using
                // the value in the Number field in xml file
                } else if (!dbHelp.isEmployee(String.valueOf(waiter_id))){
                    Toast.makeText(OrderActivity.this, "Id not valid", Toast.LENGTH_SHORT).show();
                }

              

        });

查询数据库DataBaseHelper类的方法

public Boolean isEmployee(String id){
    SQLiteDatabase db = this.getReadableDatabase();

    String findEmployeeUsingId = "SELECT * FROM " + EMP_TABLE +
            " WHERE " + ID_EMP + " = " + id;
    Cursor cursor = db.rawQuery(findEmployeeUsingId, null);


    if (cursor.getCount() == 0) {
        return false;
    }
    else
        return true;
}

我收到的错误信息是:

com.example.dropit E/SQLiteLog: (1) near ".": syntax error in "SELECT * FROM EMP_TABLE WHERE ID = androidx.appcompat.widget.AppCompatEditText{93d8cbb VFED..CL. .F...... 249,193-829,317 #7f08011f app:id/waiter_id aid=1073741824}"

android.database.sqlite.SQLiteException: near ".": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM EMP_TABLE WHERE ID = androidx.appcompat.widget.AppCompatEditText{93d8cbb VFED..CL. .F...... 249,193-829,317 #7f08011f app:id/waiter_id aid=1073741824}

    at com.example.dropit.DataBaseHelper.isEmployee(DataBaseHelper.java:283)
    at com.example.dropit.OrderActivity$1.onClick(OrderActivity.java:58)

【问题讨论】:

  • 第一次使用参数,第二次调试String.valueOf(waiter_id))返回的内容
  • 请解释否决票,它返回String
  • 什么字符串?...显然不是你想要的在我调用在 DataBaseHelper isEmployee(String id) 类中创建的方法后,该方法使用用户输入的 id 进行搜索 这不是真的...检查字符串
  • String 表示 int 参数,这是我想要的,它是已输入的用户 id 的数量
  • int 参数的字符串表示 不,它不是...请调试你的程序拳头

标签: java android sqlite android-sqlite


【解决方案1】:

waiter_idEditText 而不是字符串。
当你调用isEmployee() 时,你应该传递它的文本:

else if (!dbHelp.isEmployee(waiter_id.getText().toString()))

同时在rawQuery() 中使用? 占位符,而不是连接参数:

String findEmployeeUsingId = "SELECT * FROM " + EMP_TABLE + " WHERE " + ID_EMP + " = ?";
Cursor cursor = db.rawQuery(findEmployeeUsingId, new String[] {id});

【讨论】:

  • 谢谢,我明白我认为因为input 类型是Number,所以它是int,但感谢您澄清它实际上是text
猜你喜欢
  • 2016-05-30
  • 2021-04-25
  • 2021-12-04
  • 1970-01-01
  • 2019-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多