我有下表。我从JSON获取此数据并将其存储到SQLite表中。我想从TABLE_MAC
获取所有数据,其中meal_id = 12。
TABLE_MAC
| _id | meal_id | name |
----------------------------
| 1 | 12,16,17| mac veggi|
| 2 | 14,16 | mac allo |
| 3 | 16,12,14| mac egg |
| 4 | 112,1,14| fries |
现在,我想从数据库中获取来自meal_id = 12的所有数据。我已应用LIKE
运算符从数据库中获取数据,但它在Android中添加了fries
。
答案 0 :(得分:3)
致电 LIKE 运营商
public List<Model> getAllResult(String getId)
{
List<Model> resultList = new ArrayList<Model>();
// Select All Query
String selectQuery = "SELECT * FROM "+ TABLE_MAC + " WHERE " + KEY_Meal_ID + " LIKE '%"+getId+"%'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst())
{
do {
Model sg = new Model();
sg.set_id((cursor.getString(0)));
......
resultList.add(sg);
} while (cursor.moveToNext());
cursor.close();
db.close();
}
// return list
return resultList;
}
答案 1 :(得分:1)
尝试这样的方法来查询数据库
SQLiteDatabase database = this.getWritableDatabase();
String[] columns = new String[] {"_id", "meal_id", "name"};
String TABLE_NAME = "TABLE_MAC";
Cursor mCursor =
database.query(true, TABLE_NAME, columns,
"meal_id=12", null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
然后,您必须循环浏览Cursor
以检索您的数据。
在您的meal_id
列中使用逗号分隔ID并不是一个好主意。您应该实现映射表或重新考虑数据库以避免这种情况。