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)
waiter_id
is an EditText
and not a string.
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});