-1

我正在尝试从android向mysql db插入多行,但我只知道如何插入一行

php脚本:

$r=mysql_query($sql);
if(!$r)
echo "Error in query: ".mysql_error();
mysql_close();
?>

android文件的一部分:

    param = new ArrayList<NameValuePair>();
    param.add(new BasicNameValuePair("c_name", city));
    param.add(new BasicNameValuePair("s_name", street));

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url_select);

如果我向参数数组列表添加更多条目,它会起作用吗?在循环中?就像是

for (String[] s : examp){
    param.add(new BasicNameValuePair("c_name", s[0]));
    param.add(new BasicNameValuePair("s_name", s[1]));
}

或者我该怎么做?谢谢。

4

1 回答 1

0

好的,我做的可能不是很干净,但它有效

我在循环中从光标获取数据,每次调用上传函数时

从游标数据库获取数据 db = new Database(this); 光标 c = db.getSpolecnostiUpSyn();

 int sp_id;
 String sp_spolecnost;

if (c != null ) {

if  (c.moveToFirst()) {
    do {
        sp_id = c.getInt(c.getColumnIndex(Database.COLUMN_ID_SPOLECNOST));
        sp_spolecnost = c.getString(c.getColumnIndex(Database.COLUMN_SPOLECNOST_SPOLECNOST));

        spolecnostNahraj(sp_id, sp_spolecnost); // for upload data each row
        Log.d("up spolecnost", "nahrano");
       }while (c.moveToNext());
   } 
}
}

并上传

void spolecnostNahraj(int idspolecnosti,  String spolecnost){   

JSONArray jArray;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
nameValuePairs.add(new BasicNameValuePair("spolecnost",spolecnost));

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://***.cz/syn/****.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
    Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
  sb = new StringBuilder();
  sb.append(reader.readLine() + "\n");

  String line="0";
  while ((line = reader.readLine()) != null) {
                 sb.append(line + "\n");
   }
   is.close();
   result=sb.toString();
   }catch(Exception e){
         Log.e("log_tag", "Error converting result "+e.toString());
   }
//paring data
int sp_id;
try{
 jArray = new JSONArray(result);
 JSONObject json_data=null;

 Database db = new Database(this);

 for(int i=0;i<jArray.length();i++){
        json_data = jArray.getJSONObject(i);
        sp_id=json_data.getInt("ID_Spol_Pumpa");

        db.opravOdSpolecnosti(idspolecnosti, sp_id);

        Log.d("id spolecnosti vlozenyho", String.valueOf(sp_id));

    }
 }
 catch(JSONException e1){
  Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show();
 } catch (ParseException e1) {
        e1.printStackTrace();
}

}

我希望能帮助别人

于 2013-04-08T00:38:21.543 回答