accessing mysql db
in
Android Processing
•
2 years ago
Hi, i've found a very nice script to access the mysql database. Now I want to use this script. I've adapted some php to make it working. Here's the PHP:
- <?php
- define("DBHOST", "localhost");
- define("DBNAME", "name");
- define("DBLOGIN", "login");
- define("DBPW", "password");
- function dbConnect() {
- $db = mysql_connect(DBHOST, DBLOGIN, DBPW) or die ("Could not connect to database".mysql_error());
- mysql_select_db(DBNAME) or die ("could not select database");
- return $db;
- }
- function dbClose() {
- mysql_close();
- }
- dbConnect();
- $query = "SELECT * FROM dbLogin";
- $result = mysql_query($query) or die("query failed: " . mysql_error());
- // print_r($query);
- $data = array();
- while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
- $data[] = $row;
- }
- print(json_encode($data));
- dbClose();
- ?>
This is the android script:
- //package com.list;
- //import java.io.BufferedReader;
- //import java.io.InputStream;
- //import java.io.InputStreamReader;
- import java.util.ArrayList;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.apache.http.message.BasicNameValuePair;
- import android.app.ListActivity;
- import android.net.ParseException;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.InputStreamReader.*;
- void mysqlDB() {
- String result = "";
- //the year data to send
- ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
- nameValuePairs.add(new BasicNameValuePair("year", "1980"));
- //http post
- try {
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("http://yourhost/read.PHP");
- httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- InputStream 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, "iso-8859-1"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- 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());
- }
- //parse json data
- try {
- JSONArray jArray = new JSONArray(result);
- for (int i=0;i<jArray.length();i++) {
- JSONObject json_data = jArray.getJSONObject(i);
- Log.i("log_tag", "id: "+json_data.getString("id")+
- ", longitude: "+json_data.getString("lon")+
- ", latitude: "+json_data.getString("lat")+
- ", concept: "+json_data.getString("concept")
- );
- }
- }
- catch(JSONException e) {
- Log.e("log_tag", "Error parsing data "+e.toString());
- }
- }
And i get a error at this part:
- BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
- BUILD FAILED
- /Users/martijnmellema-1/android-sdk/tools/ant/main_rules.xml:384: Compile failed; see the compiler error output for details.
- Total time: 2 seconds
1