从Android应用程序更新数据库中的行

时间:2014-10-14 23:20:20

标签: php android mysql android-asynctask sql-update

我正在尝试通过我的Android应用更新数据库中的一行。我从数据库中取出工作正在运行,但我在更新方面遇到了麻烦。我的更新代码如下:

我的AsyncTask类:

    private class UpdateAnimalTask extends AsyncTask<Void, Void, Boolean>
 {   
     @Override
     protected Boolean doInBackground(Void... arg0)
     {
            try
            { 
                ID = (EditText) findViewById(R.id.eTid);
                Name = (EditText) findViewById(R.id.eTname);
                Type = (EditText) findViewById(R.id.eTtype);
                Breed = (EditText) findViewById(R.id.eTbreed);
                Gender = (EditText) findViewById(R.id.eTgender);
                Injuries = (EditText) findViewById(R.id.eTinjuries);
                Treat = (EditText) findViewById(R.id.eTtreat);

                String nM = Name.getText().toString();
                String tP = Type.getText().toString();
                String bR = Breed.getText().toString();
                String gE = Gender.getText().toString();
                String iN = Injuries.getText().toString();
                String tR = Treat.getText().toString();

                ArrayList<NameValuePair> up = new ArrayList<NameValuePair>();
                up.add(new BasicNameValuePair("name", nM));
                up.add(new BasicNameValuePair("type", tP));
                up.add(new BasicNameValuePair("breed", bR));
                up.add(new BasicNameValuePair("gender", gE));
                up.add(new BasicNameValuePair("injuries", iN));
                up.add(new BasicNameValuePair("treatment", tR));

                String phpLink = "http://select.garethprice.co.za/update.php?name=" + nM;
                Log.e("Test", up.toString());
                dbUpdate(up, phpLink);
                Log.e("Test", up.toString());
            }
            catch(Exception e)
            {
                Log.e("log_tag", "Error in uploading " + e.toString());
                Toast.makeText(getBaseContext(), "Error " + e.toString(), Toast.LENGTH_LONG).show();
                return false;
            }
            return true;
     }
     @Override
     protected void onPostExecute(Boolean result)
     {
         if(result)
         {
             Toast.makeText(getBaseContext(), "Successfully updated", Toast.LENGTH_LONG).show();
         }
     }
 }

我的asynctask类中调用的dbUpdate方法:

    public void dbUpdate(ArrayList<NameValuePair> data, String phpL)
{
    InputStream iS = null;

    try
    {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(phpL);
        httppost.setEntity(new UrlEncodedFormEntity(data));
        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());
        Toast.makeText(getBaseContext(), "Error " + e.toString(), Toast.LENGTH_LONG).show();
    }
}

我的php:

    <?php
          include_once 'db.php'; 
          $con=mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)or die("cannot connect"); 
          mysql_select_db(DB_DATABASE)or die("cannot select DB");
          $nM = $_GET['name'];
          $tP = $_POST['type'];
          $bR = $_POST['breed'];
          $gE = $_POST['gender'];
          $iN = $_POST['injuries'];
          $tR = $_POST['treatment'];
          $sql = "UPDATE tbl_Animals SET animal_Type = '$tP', animal_Breed = '$bR', animal_Gender = '$gE', animal_Injuries = '$iN', animal_Treatments = '$tR' WHERE animal_Name = '$nM'"; 
          echo "test: " . $nM . $tP . $bR . $gE . $iN . $tR;
          mysql_query($sql,$con) or die("error: " . mysql_error());
          mysql_close($con)
    ?>

在我的更新按钮中执行asynctask:

    public void Update(View v)
        {
            new UpdateAnimalTask().execute();
        }

Android代码没有破坏所以我怀疑它是与php的东西,因为我的toast弹出,说我的onPostExecute更新成功。 提前谢谢。

0 个答案:

没有答案