在将订单添加到数据库之前检查是否满足条件
Checking that conditions are met before adding the order to the database
我正在尝试确保用户选择了服务员 ID(第一个 if 语句)并且该 id 有效并且与数据库中的记录相关(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)
class 中创建的方法后,该方法使用用户输入的 id
在数据库中搜索该记录。如果在数据库中找不到该 ID,则应显示一条消息提醒用户。
点击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的方法Class
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.onClick(OrderActivity.java:58)
waiter_id
是一个 EditText
而不是字符串。
当您调用 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});
我正在尝试确保用户选择了服务员 ID(第一个 if 语句)并且该 id 有效并且与数据库中的记录相关(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)
class 中创建的方法后,该方法使用用户输入的 id
在数据库中搜索该记录。如果在数据库中找不到该 ID,则应显示一条消息提醒用户。
点击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的方法Class
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.onClick(OrderActivity.java:58)
waiter_id
是一个 EditText
而不是字符串。
当您调用 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});