0

我正在开发一个应用程序,其中我使用外部 Ms Access 数据库(在 PC 上)作为我的 android 应用程序的数据库。我已经使用 PHP for Android 完成了 MySql 和 MSSql 的连接,但我想知道如何继续使用 MS Access 数据库。

欢迎任何教程或片段!

4

1 回答 1

0

1)在Android端做一个这样的功能:

public void connect_access_php(String vars)
{
    List<NameValuePair> itemstring = new ArrayList<NameValuePair>();
    itemstring.add(new BasicNameValuePair("VARIABLE", vars));
    HttpClient httpclient = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000); //Timeout Limit

    try
    {
        HttpPost httppost = new HttpPost("url_of_php_file");
        httppost.setEntity(new UrlEncodedFormEntity(itemstring));
        HttpResponse response = httpclient.execute(httppost);
        InputStream content = response.getEntity().getContent();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
        String s;
        while ((s = buffer.readLine()) != null) {

            //Handle your Values here

        }

     }catch (Exception e) {
        Toast.makeText(getApplicationContext(),"Connection Problem !", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
     }
}

2)然后在php页面上,与access文件进行连接如下:

<?php
$variab = $_REQUEST['VARIABLE'];
$conn = new COM("ADODB.Connection") or die("Oops!");
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\Documents and Settings\xxxx\Desktop\access_file.mdb");
$query = "SELECT * FROM table WHERE (Variable = '".$variab."')";
$data = $conn->Execute($query);

while (!$data->EOF)
{
    echo $data ->Fields[0]->value . ":";
    //echo $data ->Fields[1]->value . " \n";

    $data ->MoveNext();
}
?>  

使用此示例,您可以将变量传递给 php 脚本,从而访问 CRUD 函数的文件。

祝你好运!!

于 2013-05-10T06:40:32.370 回答