Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access SQL server database from an phonegap android app?

I'm confused with the ways to connect SQL Server database with Android Phonegap app. I used PHP code to query the data.

Is that possible?

My app is not running. It's blank. Can anybody give me a clear example or sample code to work out this..

like image 697
Navin Kumar Avatar asked Feb 15 '26 21:02

Navin Kumar


1 Answers

Connecting android to server using PHP is best way ...

First use name value pair

        public void registerUser(String email, String password, String mobile) {
    // Building Parameters

           List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("tag", register_tag));
    params.add(new BasicNameValuePair("keyemail", email));
    params.add(new BasicNameValuePair("keypassword", password));
    params.add(new BasicNameValuePair("keymobile", mobile));

    // getting JSON Object


    JsonParser.makeHttpRequest(registerURL,params);


}

Use JSON to send the data

      public JSONObject makeHttpRequest(String url,
        List<NameValuePair> params) {


    // Making HTTP request
    try {




            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();





    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
      }

    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();
        json = sb.toString();


    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {

        jObj = new JSONObject(json);
        Log.d("Parser", "IN try parse the string to a JSON object");
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

PHP code will get this value and use further

  // include db connect class
  require_once dirname(__FILE__).'/db_connect.php';

  // connecting to db
  $db = new DB_CONNECT();

  $response = array();



  $email = $_POST['keyemail'];
  $password= $_POST['keypassword'];
 $phone=$_POST['keymobile'];
 $tag=$_POST['tag'];



$result=mysql_query("INSERT INTO TableName (register_email,password,mobile)    VALUES('$email','$password', '$phone')");

if ($result) {
    // successfully updated
    $response["success"] = 1;
    $response["message"] = "Data Inserted Successfully.";

    // echoing JSON response
    echo json_encode($response);
} else {
    $response["error_msg"]="Error In Insertion";
}

?>

like image 90
Rohan Kadu Avatar answered Feb 18 '26 14:02

Rohan Kadu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!