[Switch] connect to the MySQL database (android, php, MySQL) and mysqlandroid
The simplest and most convenient way to manage MySQL databases is PHP scripts.
Run the PHP script to connect to the android system using the HTTP protocol.
We encode data in JSON format because Android and PHP both have ready-made JSON functions.
The following sample code reads data from the database according to the given conditions and converts the data to JSON data.
The HTTP protocol is used to parse JSON data for android devices.
MySQL has the following tables and some data.
1 create table 'People' (2 'id' int not null AUTO_INCREMENT primary key, 3 'name' VARCHAR (100) not null, 4 'sex 'bool not null default '1', 5 'birthyear' int not NULL6)View Code
We want to obtain the data of a person born in a specified year.
PHP code will be very simple: connect to the database -- run an SQL query, get data according to the WHERE statement block set -- the conversion is output in JSON format
For example, we will have this function getAllPeopleBornAfter. php file:
1 <? Php 2/* connect to the database */3 mysql_connect ("host", "username", "password"); 4 mysql_select_db ("leledata "); 5/* $ _ REQUEST ['Year'] to obtain the annual value sent by Android, concatenate an SQL query statement */6 $ q = mysql_query ("SELECT * FROM people WHERE birthyear> '". $ _ REQUEST ['Year']. "'"); 7 while ($ e = mysql_fetch_assoc ($ q) 8 $ output [] = $ e; 9/* output in JSON format */10 print (json_encode ($ output); 11 12 mysql_close (); 13?>View Code
The Android part is a little more complicated: Use HttpPost to send the annual value, get the data -- convert the response string -- parse the JSON data.
Use it.
1 String result = ""; 2/* set the sent annual value */3 ArrayList <NameValuePair> nameValuePairs = new ArrayList <NameValuePair> (); 4 nameValuePairs. add (new BasicNameValuePair ("year", "1980"); 5 6/* annual value sent with HttpPost */7 try {8 HttpClient httpclient = new DefaultHttpClient (); 9 HttpPost httppost = new HttpPost ("http://example.com/getAllPeopleBornAfter.php"); // The above php URL10 httppost. setEntity (new UrlEncodedFormEntity (na MeValuePairs); 11 HttpResponse response = httpclient.exe cute (httppost); 12 HttpEntity entity = response. getEntity (); 13 InputStream is = entity. getContent (); 14} catch (Exception e) {15 Log. e ("log_tag", "network error" + e. toString (); 16} 17/* Conversion response string */18 try {19 BufferedReader reader = new BufferedReader (new InputStreamReader (is, "iso-8859-1"), 8 ); // note that "iso-8859-1" encoding does not support Chinese characters. 20/* to support Chinese characters, set MySQL to UTF8 format, and set UTF8 to read */21 StringBuilder sb = new StringBuilder (); 22 String line = null; 23 while (line = reader. readLine ())! = Null) {24 sb. append (line + "\ n"); 25} 26 is. close (); 27 28 result = sb. toString (); 29} catch (Exception e) {30 Log. e ("log_tag", "conversion response string error" + e. toString (); 31} 32 33/* parse JSON data */34 try {35 JSONArray jArray = new JSONArray (result); 36 for (int I = 0; I <jArray. length (); I ++) {37 JSONObject json_data = jArray. getJSONObject (I); 38 Log. I ("log_tag", "id:" + json_data.getInt ("id") + 39 ", name:" + json_data.getString ("name") + 40 ", sex: "+ json_data.getInt (" sex ") + 41", birthyear: "+ json_data.getInt (" birthyear ") 42); 43} 44} 45} catch (JSONException e) {46 Log. e ("log_tag", "JSON data parsing error" + e. toString (); 47}View Code
Note: The online code above android4.0 can only be placed in sub-threads