【问题标题】:how insert multiple rows to mysql over json from android?如何从android通过json向mysql插入多行?
【发布时间】:2013-04-06 17:06:59
【问题描述】:

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

php 脚本:

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

部分安卓文件:

    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]));
}

或者我该怎么做?谢谢。

【问题讨论】:

    标签: android mysql json


    【解决方案1】:

    好的,我做的可能不是很干净,但它可以工作

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

    从游标中获取数据 数据库 db = 新数据库(此); 光标 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();
    }
    
    }
    

    希望对大家有所帮助

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 2021-02-13
      • 2016-04-30
      • 2015-02-17
      • 2021-11-28
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多