我在代码中使用PreparedStatement
进行查询。例如:
PreparedStatement stmt = db.con.prepareStatement("select id from nodes where x>? and x<? and y>? and y<?");
stmt.setDouble(1, x1);
... //set a value for each param 1 thru 4
现在我有另一个想要使用上面完全相同的查询作为子查询的查询。所以我能做到:
PreparedStatement stmt2 = db.con.prepareStatement("select id from edges where startNodeId in
(select id from nodes where x>? and x<? and y>? and y<?)");
但这是重复的,我可能会修改第一个PreparedStatement
,并希望这些更改传播到第二个。stmt2.setPreparedStatement(2, stmt)
。有没有办法在另一个语句中将预准备语句设置为子查询?
也许类似于class Request extends AsyncTask<Void, Void, Response> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
}
protected Response doInBackground(Void... urls) {
try {
URL url = new URL(API_URL + "email=" +URLEncoder.encode(Email, "utf-8") + "&password=" + URLEncoder.encode(Password, "utf-8"));
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
Log.d("Response Code: ", String.valueOf(urlConnection.getResponseCode()));
Log.d("Error Body: ", String.valueOf(urlConnection.getErrorStream()));
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
//Parse Response
Log.d("Response", stringBuilder.toString());
JSONObject object = (JSONObject) new JSONTokener(stringBuilder.toString()).nextValue();
Response res = new Response();
res.setStatus(object.getString("status"));
res.setUserID(object.getString("UserID"));
res.setError(object.getString("error"));
Log.d("Req Response obj Status",res.getStatus() );
return res;
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
}
}
?
答案 0 :(得分:1)
只需将两个查询合并到一个SELECT
语句中即可。
SELECT x,y,z FROM tablez WHERE id IN (SELECT id FROM "your first query")