如何使用android代码连接mysql数据库并将数据插入其中

时间:2014-08-08 17:08:40

标签: android mysql database

我的托管服务器上有mysql database

simple android applicationfeedback formsubmit我希望insert data into mysql database在服务器上。

tried google并在solution for local machine

后面找到了这个
  

如何在没有任何php代码的情况下连接到我的托管服务器和mysql数据库?

public void insert()
    {
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("id",id));
    nameValuePairs.add(new BasicNameValuePair("name",name));

        try
        {
        HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/insert.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost); 
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            Log.e("pass 1", "connection success ");
    }
        catch(Exception e)
    {
            Log.e("Fail 1", e.toString());
            Toast.makeText(getApplicationContext(), "Invalid IP Address",
            Toast.LENGTH_LONG).show();
    }     

        try
        {
            BufferedReader reader = new BufferedReader
            (new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
        {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        Log.e("pass 2", "connection success ");
    }
        catch(Exception e)
    {
            Log.e("Fail 2", e.toString());
    }     

    try
    {
            JSONObject json_data = new JSONObject(result);
            code=(json_data.getInt("code"));

            if(code==1)
            {
        Toast.makeText(getBaseContext(), "Inserted Successfully",
            Toast.LENGTH_SHORT).show();
            }
            else
            {
         Toast.makeText(getBaseContext(), "Sorry, Try Again",
            Toast.LENGTH_LONG).show();
            }
    }
    catch(Exception e)
    {
            Log.e("Fail 3", e.toString());
    }
    }

1 个答案:

答案 0 :(得分:4)

下面

HttpPost httppost = new HttpPost("http://10.0.2.2/insert.php");
提到

insert.php意味着你必须把这个文件放在服务器上

只需将http://10.0.2.2/insert.php更改为存储文件的服务器文件路径

insert.php的代码       

        // this variables is used for connecting to database and server
        $host="yourhost";
        $uname="username";
        $pwd='pass';
        $db="dbname";

        // this is for connecting
        $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
        mysql_select_db($db,$con) or die("db selection failed");

        // getting id and name from the client
         if(isset($_REQUEST)){
        $id=$_REQUEST['id'];
        $name=$_REQUEST['name'];}


       // variable used to tell the client whether data is stored in database or not

        $flag['code']=0;

        // for insertion
        if($r=mysql_query("insert into emp_info values('$name','$id') ",$con))
        {
            //if insertion succeed set code to 1
            $flag['code']=1;
            echo"hi";
        }
        // send result to client that will be 1 or 0
        print(json_encode($flag));

        //close
        mysql_close($con);
    ?>

如您的代码中所述,这将从服务器获取数据是否存储数据,代码= 1表示存储,代码= 0表示未存储

 JSONObject json_data = new JSONObject(result);
            code=(json_data.getInt("code"));

            if(code==1)
           {
        Toast.makeText(getBaseContext(), "Inserted Successfully",
            Toast.LENGTH_SHORT).show();
            }
            else
          {
        Toast.makeText(getBaseContext(), "Sorry, Try Again",
            Toast.LENGTH_LONG).show();
            }