问题描述
我有 mysql数据库
我的托管服务器上
在简单的Android应用程序
我反馈表单
和提交
我想将数据插入到mysql数据库
这是对服务器。
我试图谷歌
,发现这下本地机解决方案
公共无效插入()
{
ArrayList的<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>();
nameValuePairs.add(新BasicNameValuePair(ID,ID));
nameValuePairs.add(新BasicNameValuePair(姓名,名字));
尝试
{
HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(http://10.0.2.2/insert.php);
httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpclient.execute(httppost);
HttpEntity实体= response.getEntity();
是= entity.getContent();
Log.e(通1,连接成功);
}
赶上(例外五)
{
Log.e(失败1,e.toString());
Toast.makeText(getApplicationContext(),无效的IP地址,
Toast.LENGTH_LONG).show();
}
尝试
{
的BufferedReader读卡器=新的BufferedReader
(新的InputStreamReader(是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
而((行= reader.readLine())!= NULL)
{
sb.append(行+\ N);
}
is.close();
结果= sb.toString();
Log.e(传2,连接成功);
}
赶上(例外五)
{
Log.e(失败2,e.toString());
}
尝试
{
JSONObject的json_data =新的JSONObject的(结果);
code =(json_data.getInt(code));
如果(code == 1)
{
Toast.makeText(getBaseContext(),插入成功,
Toast.LENGTH_SHORT).show();
}
其他
{
Toast.makeText(getBaseContext(),对不起,请重试,
Toast.LENGTH_LONG).show();
}
}
赶上(例外五)
{
Log.e(失败3,e.toString());
}
}
下面
HttpPost httppost =新HttpPost(http://10.0.2.2/insert.php);
中提到insert.php意味着你必须把这个文件在服务器
只是改变了 http://10.0.2.2/insert.php
到服务器文件的路径所在的文件存放的路径
$ C $下insert.php
//这个变量用于连接到数据库和服务器
$主机=yourhost;
$的uname =用户名;
$ PWD =通行证;
$ DB =DBNAME;
//这是用于连接
$ CON =的mysql_connect($主机,$ UNAME,$ PWD)或死亡(连接失败);
mysql_select_db($数据库,$ CON)或死亡(DB选择失败);
//获取ID和名称从客户端
如果(使用isset($ _ REQUEST)){
的$ id = $ _ REQUEST ['身份证'];
$名称= $ _ REQUEST ['名称'];}
//变量用来告诉客户端数据是否存储在数据库或不
$标志['code'] = 0;
//插入
如果($ R =请求mysql_query(插入emp_info值('$名字','的$ id'),$ CON))
{
//如果插入成功,集code至1
$标志['code'] = 1;
回声嗨;
}
//发送结果到客户端,这将是1或0
打印(json_en code($标志));
//关闭
则mysql_close($ CON);
?>
中所提到的code,这将获得从服务器的值的数据是否存储或不code = 1存储和$ C $使c = 0为没有存储
的JSONObject json_data =新的JSONObject的(结果);
code =(json_data.getInt(code));
如果(code == 1)
{
Toast.makeText(getBaseContext(),插入成功,
Toast.LENGTH_SHORT).show();
}
其他
{
Toast.makeText(getBaseContext(),对不起,请重试,
Toast.LENGTH_LONG).show();
}
I have mysql database
on my hosting server
On simple android application
I have feedback form
and on submit
I want to insert data into mysql database
which is on server .
I tried google
and found this following solution for local machine
public void insert()
{
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id",id));
nameValuePairs.add(new BasicNameValuePair("name",name));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
Toast.makeText(getBaseContext(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
}
Here
HttpPost httppost = new HttpPost("http://10.0.2.2/insert.php");
insert.php is mentioned means you have to put this file on server
just change the http://10.0.2.2/insert.php
to the path of your server file path where the file is stored
Code for insert.php
// this variables is used for connecting to database and server
$host="yourhost";
$uname="username";
$pwd='pass';
$db="dbname";
// this is for connecting
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
// getting id and name from the client
if(isset($_REQUEST)){
$id=$_REQUEST['id'];
$name=$_REQUEST['name'];}
// variable used to tell the client whether data is stored in database or not
$flag['code']=0;
// for insertion
if($r=mysql_query("insert into emp_info values('$name','$id') ",$con))
{
//if insertion succeed set code to 1
$flag['code']=1;
echo"hi";
}
// send result to client that will be 1 or 0
print(json_encode($flag));
//close
mysql_close($con);
?>
as mentioned in your code , this will get the value from server whether the data is stored or not by code=1 for stored and code = 0 for not stored
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
Toast.makeText(getBaseContext(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
这篇关于我如何连接mysql数据库,并采用了android code将数据插入到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!