【问题标题】:How to access SQL server database from an phonegap android app?如何从 phonegap android 应用程序访问 SQL Server 数据库?
【发布时间】:2013-09-28 12:49:46
【问题描述】:

我对将 SQL Server 数据库与 Android Phonegap 应用程序连接的方式感到困惑。我使用PHP 代码查询数据。

这可能吗?

我的应用程序没有运行。它是空白的。谁能给我一个清晰的例子或示例代码来解决这个问题..

【问题讨论】:

  • 使用 Web 服务,而不是直接与移动应用程序中的数据库建立连接。
  • @TGMCians 嘿,我是这个新手,你能详细解释一下或任何链接,示例项目..something!!!

标签: android sql-server json eclipse cordova


【解决方案1】:

使用 PHP 将 android 连接到服务器是最好的方法 ...

首次使用名称值对

        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);


}

使用 JSON 发送数据

      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 代码将获取该值并进一步使用

  // 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";
}

?>

【讨论】:

  • 我可能错了,但这看起来像是我回答中教程中修改后的复制和粘贴。如果您阅读下面我回答中的教程,它将向您解释一切。
  • @Mark Winterbottom 但该教程使用 mysql 进行了解释,当我使用 ms sql 时会出现任何问题吗???
猜你喜欢
  • 1970-01-01
  • 2011-12-25
  • 2014-06-12
  • 2013-08-18
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 2014-05-01
相关资源
最近更新 更多