Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从SQLite数据库检索行中的特定数据_Android_Sqlite - Fatal编程技术网

Android 从SQLite数据库检索行中的特定数据

Android 从SQLite数据库检索行中的特定数据,android,sqlite,Android,Sqlite,我有下表。我从JSON获取了这些数据,并将其存储到SQLite表中。我想从TABLE\u MAC中获取所有数据,其中fine\u 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

我有下表。我从JSON获取了这些数据,并将其存储到SQLite表中。我想从
TABLE\u MAC
中获取所有数据,其中fine\u 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    |

现在,从一个数据库中,我想从deal_id=12获取所有数据。我应用了
LIKE
操作符从数据库获取数据,但它在Android中添加了
fries

调用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;
    }
public List getAllResult(字符串getId)
{
List resultList=new ArrayList();
//选择所有查询
String selectQuery=“SELECT*FROM”+TABLE\u MAC+,其中“+KEY\u fine\u ID+”类似“%”“+getId+“%”;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst())
{
做{
模型sg=新模型();
sg.set_id((cursor.getString(0));
......             
结果列表添加(sg);
}while(cursor.moveToNext());
cursor.close();
db.close();
}
//返回列表
返回结果列表;
}

尝试类似的方法来查询您的数据库

    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();
    }
然后,您必须循环通过
光标
来检索数据。
在您的
用餐id
列中使用逗号分隔的id不是一个好主意。您应该实现一个映射表或重新考虑您的数据库以避免这种情况。

好吧,
112
就像“
12
。。。不要将逗号分隔的列表存储在一列中。这到底是怎么回事<根据问题需要使用LIKE,代码>密钥ID不等于
12
。等等,但这就是问题中提到的问题;)LIKE不起作用
LIKE“%”+getID+“%”
这也将取112的值。这将返回0行,因为第一行中的dine_id=“12,16,17”与此类似