我的Android应用程序中的外部Ms Access数据库连接

时间:2013-04-25 06:28:56

标签: android database ms-access

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

欢迎使用任何教程或片段!

1 个答案:

答案 0 :(得分:0)

1)在Android Side上创建一个这样的函数:

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页面上,按如下方式与访问文件建立连接:

<?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函数的文件。

最好的运气!!