英文:
How to search on SQLITE database android using OR condition?
问题
public List<Contacts> search(String keyword) {
List<Contacts> contacts = null;
try {
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + TABLE_CONTACTS + " where (" + COLUMN_NAME + " or " + COLUMN_CODE + " or " + COLUMN_PNO + ") like ?", new String[]{"%" + keyword + "%"});
if (cursor.moveToFirst()) {
contacts = new ArrayList<>();
do {
int id = Integer.parseInt(cursor.getString(0));
String cardNo = cursor.getString(1);
String name = cursor.getString(2);
String jacode = cursor.getString(3);
String phno = cursor.getString(4);
Contacts contact = new Contacts();
contact.setId(id);
contact.setCardID(cardNo);
contact.setName(name);
contact.setJacode(jacode);
contact.setPhno(phno);
contacts.add(new Contacts(id, cardNo, name, jacode, phno, cplan, rent, balance, zone, oldBalance, paidDate, paidAmount, status));
} while (cursor.moveToNext());
}
} catch (Exception e) {
contacts = null;
}
return contacts;
}
Note: I've provided the translated code you requested. If you have any further questions or need assistance with this code, please feel free to ask.
英文:
I am having trouble with searching on sqlite android.
My input types are Name, PhoneNumber,Code.
What I want is whenever the user type any of the following type of input in the search bar it should check whether it is present on any of the given column and return the value.
For example, If the user input the name ALAN then ALAN should return. If the user input the code Jaxxx1 then it should search for the value Jaxxx1 and return its corresponding data. If the user search using phone number then it should return its corresponding data.
What I am getting is nothing.
Below is my code somebody please help me.
public List<Contacts> search(String keyword) {
List<Contacts> contacts = null;
try {
SQLiteDatabase sqLiteDatabase = getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + TABLE_CONTACTS + " where (" + COLUMN_NAME+" or "+COLUMN_CODE +" or "+COLUMN_PNO + ") like ?", new String[]{"%" + keyword + "%"});
if (cursor.moveToFirst()) {
contacts = new ArrayList<>();
do {
int id = Integer.parseInt(cursor.getString(0));
String cardNo = cursor.getString(1);
String name = cursor.getString(2);
String jacode = cursor.getString(3);
String phno = cursor.getString(4);
Contacts contact = new Contacts();
contact.setId(id);
contact.setCardID(cardNo);
contact.setName(name);
contact.setJacode(jacode);
contact.setPhno(phno);
// Toast.makeText(context,contact.getBalance(),Toast.LENGTH_SHORT).show();
contacts.add(new Contacts(id, cardNo, name, jacode, phno, cplan, rent, balance, zone, oldBalance, paidDate, paidAmount, status));
} while (cursor.moveToNext());
}
} catch (Exception e) {
contacts = null;
}
return contacts;
}
I'm a newbie here, please help me
答案1
得分: 2
I think you made a mistake in this line
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + TABLE_CONTACTS + " where (" + COLUMN_NAME+" or "+COLUMN_CODE +" or "+COLUMN_PNO + ") like ?", new String[]{"%" + keyword + "%"});
Your statement should be like this:
"select * from " + TABLE_CONTACTS + " where " + COLUMN_NAME + " like ?" + " or " + COLUMN_CODE + " like ?" + " or " + COLUMN_PNO +" like ?", new String[]{"%" + keyword + "%","%" + keyword + "%","%" + keyword + "%"});
Please check statement (if I forgot double quotation mark). And to get the result you should always pay attention to what user type(upper case or lower case) so you can use some functions already built-in in SQLite.
英文:
I think you made a mistake in this line
Cursor cursor = sqLiteDatabase.rawQuery("select * from " + TABLE_CONTACTS + " where (" + COLUMN_NAME+" or "+COLUMN_CODE +" or "+COLUMN_PNO + ") like ?", new String[]{"%" + keyword + "%"});
Your statement should be like this:
"select * from " + TABLE_CONTACTS + " where " + COLUMN_NAME + " like ?" + " or " + COLUMN_CODE + " like ?" + " or " + COLUMN_PNO +" like ?", new String[]{"%" + keyword + "%","%" + keyword + "%","%" + keyword + "%"});
Please check statement (if I forgot double quotation mark). And to get the result you should always pay attention to what user type(upper case or lower case) so you can use some functions already built-in in SQLite.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论