我有两张桌子说
表A
________________________
|ADTMeasurement|ADTid |
|--------------|----- |
|measurement |1~1~3 |
|measurement |1~1~12 |
|measurement |1~1~7 |
|measurement |1~1~11 |
|measurement |1~1~99 |
------------------------
表B
________________________________
|ADTName |ADTid |ADTType |
|------------|-------|---------|
|ADTName_1 |1~1~3 |DTType_1 |
|ADTName_2 |1~1~12 |DTType_2 |
|ADTName_4 |1~1~7 |DTType_3 |
|ADTName_4 |1~1~11 |DTType_4 |
|ADTName_5 |1~1~99 |DTType_5 |
|ADTName_6 |1~1~10 |DTType_3 |
|ADTName_7 |1~1~4 |DTType_4 |
|ADTName_7 |1~1~6 |DTType_5 |
--------------------------------
如何从tableB中检索选定的行;对于所有" ADTid"来自tableA。我需要将结果保存在JSONarray中,以便我可以将它发送到服务器。
以下是我厌倦使用的代码,这是错误的。
public Cursor getSavedMeasuremnet() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCursor = db.query("SELECT * FROM TableB WHERE ADTId IN (SELECT ADTId FROM TableA WHERE ADTMeasurment = 'measurement'");
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
我收到错误消息 - 无法解析方法“query(java.lang.string)”
答案 0 :(得分:0)
没有只有String作为参数的查询方法,你必须传递方法中的所有参数,如下所述
public JSONArray getSavedMeasuremnet() {
JSONArray jsonArray=new JSONArray();
SQLiteDatabase db = this.getWritableDatabase();
Cursor mCursor = db.rawQuery("SELECT * FROM TableB JOIN TableA ON TableA.ADTid == TableB.ADTid where TableA.ADTMeasurement='measurement'",null)
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
try {
JSONObject jsonObject=new JSONObject();
jsonObject.put("ADTName",cursor.getString(cursor.getColumnIndex("ADTName")));
jsonObject.put("ADTid",cursor.getString(cursor.getColumnIndex("ADTid")));
jsonObject.put("ADTType",cursor.getString(cursor.getColumnIndex("ADTType")));
jsonArray.put(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
cursor.moveToNext();
}
return jsonArray;
}