我想在我的Android应用程序中使用一些参数执行插入查询。
例如在php
中,它类似于: -
$query ="insert into inventoryproductrel(id, productid, sequence_no, quantity, listprice) values(?,?,?,?,?)";
$qparams = array($id,$prod['prod'],$counter,$prod['pcs'],$prod['price']);
$productadded=$adb->pquery($query,$qparams);
此处pquery
是一个功能: -
function pquery($sql, $params){
global $log, $default_charset;
$log->debug('Prepared sql query being executed : '.$sql);
$this->checkConnection();
$this->executeSetNamesUTF8SQL();
$sql_start_time = microtime(true);
$params = $this->flatten_array($params);
if (count($params) > 0) {
$log->debug('Prepared sql query parameters : [' . implode(",", $params) . ']');
}
if($this->avoidPreparedSql || empty($params)) {
$sql = $this->convert2Sql($sql, $params);
$result = &$this->database->Execute($sql);
} else {
$result = &$this->database->Execute($sql, $params);
}
$sql_end_time = microtime(true);
$this->logSqlTiming($sql_start_time, $sql_end_time, $sql, $params);
$this->lastmysqlrow = -1;
return $result;
}
我不知道如何执行此操作,即在java / android中执行带有参数的查询。
答案 0 :(得分:1)
您可以将Android应用程序的POST请求发送到相同的PHP脚本,然后在将值插入数据库之前,从POST参数中设置变量的值。
使用以下代码发出POST请求。确保从AsyncTask或单独的线程执行此操作:
public String makePOSTRequestNew(String urlString,
List<NameValuePair> nameValuePairs) {
URL url;
String response = "";
try {
url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
os, "UTF-8"));
writer.write(getQuery(nameValuePairs));
writer.flush();
writer.close();
os.close();
InputStream responseStream = new BufferedInputStream(
conn.getInputStream());
BufferedReader responseStreamReader = new BufferedReader(
new InputStreamReader(responseStream));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null) {
stringBuilder.append(line);
}
responseStreamReader.close();
response = stringBuilder.toString();
conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
getQuery()
方法从nameValuePairs:
private String getQuery(List<NameValuePair> params)
throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
这就是你可以用你想要POST的参数调用方法的方法:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("paramName",
paramValue));
String insertResponse = makePOSTRequestNew(insertURL, nameValuePairs);
答案 1 :(得分:0)
尝试这种方式希望它能帮到你,
ContentValues args = new ContentValues();
args.put("id", id);
args.put("productid", productid);
args.put("sequence_no", sequence_no);
args.put("quantity", quantity);
args.put("listprice", listprice );
try{
SQLiteDatabase database = databasehelper.getWritableDatabase();
database.insertWithOnConflict(tableName, null, args, SQLiteDatabase.CONFLICT_REPLACE);
}catch(Exception e){
e.printStackTrace();
}