发布网友 发布时间:2022-04-24 12:57
共2个回答
热心网友 时间:2022-05-03 00:17
1. 什么是WAMP Server WAMP是Windows,Apache,MySQL和PHP,Perl,Python的简称。WAMP是一个一键安装的软件,它为开发PHP,MySQL Web应用程序提供一个环境。安装这款软件你相当于安装了Apache,MySQL和PHP。或者,你也可以使用XAMP。 2. 安装和使用WAMP Server 你可以从下载WAMP,安装完成之后,可以从开始->所有程序->WampServer->StartWampServer运行该程序。 在浏览器中输入来测试你的服务器是否安装成功。同样的,也可以打开来检验phpmyadmin是否安装成功。 3. 创建和运行PHP项目 现在,你已经有一个能开发PHP和MYSQL项目的环境了。打开安装WAMP Server的文件夹(在我的电脑中,是C:\wamp\),打开www文件夹,为你的项目创建一个新的文件夹。你必须把项目中所有的文件放到这个文件夹中。 新建一个名为android_connect的文件夹,并新建一个php文件,命名为test.php,尝试输入一些简单的php代码(如下所示)。输入下面的代码后,打开,你会在浏览器中看到“Welcome,I am connecting Android to PHP,MySQL”(如果没有正确输入,请检查WAMP配置是否正确) test.php <?php echo"Welcome, I am connecting Android to PHP, MySQL"; ?>4. 创建MySQL数据库和表 在本教程中,我创建了一个简单的只有一张表的数据库。我会用这个表来执行一些示例操作。现在,请在浏览器中输入,并打开phpmyadmin。你可以用PhpMyAdmin工具创建数据库和表。 创建数据库和表:数据库名:androidhive,表:proct CREATE DATABASE androidhive; CREATE TABLE procts( pid int(11) primary key auto_increment, name varchar(100) not null, price decimal(10,2) not null, description text, created_at timestamp defaultnow(), updated_at timestamp );5. 用PHP连接MySQL数据库 现在,真正的服务器端编程开始了。新建一个PHP类来连接MYSQL数据库。这个类的主要功能是打开数据库连接和在不需要时关闭数据库连接。 新建两个文件db_config.php,db_connect.php db_config.php--------存储数据库连接变量 db_connect.php-------连接数据库的类文件 db_config.php 如果你是PHP和MySQL新手,我建议你可以先学习PHP和SQL基础知识。 6. a)在MYSQL中新建一行(创建一行新的产品) 在你的PHP项目中新建一个php文件,命名为create_proct.php,并输入以下代码。该文件主要实现在procts表中插入一个新的产品。
热心网友 时间:2022-05-03 01:35
Android客户端直接连接远程MySQL数据库的方法如下: String result = ""; //首先使用NameValuePair封装将要查询的年数和关键字绑定 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs/getAllPeopleBornAfter.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()); } //将HttpEntity转化为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()); } //将String通过JSONArray解析成最终结果 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.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyear: "+json_data.getInt("birthyear") ); } } }catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } 虽然Android开发中可以直接连接数据库,但是实际中却不建议这么做,应该使用服务器端中转下完成。