英文:
Checking that conditions are met before adding the order to the database
问题
我正在努力确保用户已选择服务员ID(第一个if语句),并且该ID有效且与数据库中的记录相关(第二个else if语句)。
我正在从“Number(Android中的GUI文本)”收集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 按钮点击事件 OrderActivity
order.setOnClickListener(new View.OnClickListener() {
Order order;
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(waiter_id.getText())) {
Toast.makeText(OrderActivity.this, "请选择服务员ID", Toast.LENGTH_SHORT).show();
} else if (!dbHelp.isEmployee(String.valueOf(waiter_id))){
Toast.makeText(OrderActivity.this, "ID无效", 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)
英文:
I am trying to make sure that the user has selected a waiter id (The first if statement) and that the id is valid and relates to a record in the database(The else if statement).
I am collecting the id
from a Number (GUI Text in Android)
and selecting the Number
value like so
EditText waiter_id;
waiter_id = (EditText) findViewById(R.id.waiter_id);
TextUtils.isEmpty(waiter_id.getText()
checks to make sure a value has been entered and displays a message if not this works.
After I am calling a method created in the DataBaseHelper isEmployee(String id)
class which uses the id
entered by the user to search the database for that record. If the id is not found in the database then a message should appear to alert the user.
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();
}
});
Method for querying the database 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;
}
The error message I am receivingis:
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)
答案1
得分: 1
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});
英文:
waiter_id
is an EditText
and not a string.<br/>
When you call isEmployee()
you should pass its text:
else if (!dbHelp.isEmployee(waiter_id.getText().toString()))
Also use a ?
placeholder inside rawQuery()
instead of concatenating the parameter:
String findEmployeeUsingId = "SELECT * FROM " + EMP_TABLE + " WHERE " + ID_EMP + " = ?";
Cursor cursor = db.rawQuery(findEmployeeUsingId, new String[] {id});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论