以下属于php代码
(1)使用数据库的第一步首先是登录数据库:
//create dataBase
//connect dataBase
$conn = mysql_connect("localhost","root","1214078831ljl");
if(!$conn){
die("connect database fali error = "+mysql_error());
}else{
}
(2)以下是创建数据库语句:
mysql_query("CREATE DATABASE revival",$conn);
(3)选择数据库,使用前必须调用的方法:
mysql_select_db("revival",$conn);(4)以下是创建表,number为自动增长column
$create_table = "CREATE TABLE person(number int NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(15),sex varchar(15),age varchar(15),weight varchar(15),height varchar(15))";
//create table and field
mysql_query($create_table);
(5)以下是一个插入数据的example
header('Access-Control-Allow-Origin:*');
$value = $_POST["data"];
$array = array();
$de_json = json_decode($value,true);
$json_count = count($de_json);
for($a = 0;$a
$array[$a]["number"] = $de_json[$a]["number"];
$array[$a]["name"] = $de_json[$a]["name"];
$array[$a]["sex"] = $de_json[$a]["sex"];
$array[$a]["age"] = $de_json[$a]["age"];
$array[$a]["weight"] = $de_json[$a]["weight"];
$array[$a]["height"] = $de_json[$a]["height"];
}
//create dataBase
//connect dataBase
$conn = mysql_connect("localhost","root","1214078831ljl");
if(!$conn){
die("connect database fali error = "+mysql_error());
}else{
}
//create database
if(mysql_query("CREATE DATABASE revival",$conn)){
}else{
}
//select database
mysql_select_db("revival",$conn);
$create_table = "CREATE TABLE person(number int NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(15),sex varchar(15),age varchar(15),weight varchar(15),height varchar(15))";
//create table and field
mysql_query($create_table);
for($a = 0;$a < $json_count;$a++){
$where = "SELECT * FROM person WHERE number = "."\"".$array[$a]["number"]."\"";
if($ok = mysql_fetch_array(mysql_query($where))){
}else{
//insert data
$insert = "INSERT INTO person(name,sex,age,weight,height) VALUES(\"" . $array[$a]["name"] . "\",";
$insert .= "\"" . $array[$a]["sex"] . "\",";
$insert .="\"" . $array[$a]["age"] . "\",";
$insert .="\"" . $array[$a]["weight"] . "\",";
$insert .="\"" . $array[$a]["height"] . "\")";
//execute command
mysql_query($insert,$conn);
}
}
?>
(6)以下是查询的一个例子:
header('Access-Control-Allow-Origin:*');
$returnData = "[";
$value = 0;
$connect = mysql_connect("localhost","root","1214078831ljl");
if(!$connect){
die("connect database fail! error = "+mysql_error());
}
mysql_select_db("revival",$connect);
//query all data sort return
$dataBaseData = mysql_query("SELECT * FROM person ORDER BY number");
//mysql_fetch_array() method transform to array
while($arrayData = mysql_fetch_array($dataBaseData)){
if($value>0){
$returnData .=",";
}
$value++;
$number = $arrayData["number"];
$name = $arrayData["name"];
$sex = $arrayData["sex"];
$age = $arrayData["age"];
$weight = $arrayData["weight"];
$height = $arrayData["height"];
//assemble json data
$returnData .="{\"number\":\"" . $number . "\",\"name\":\"" . $name . "\",\"sex\":\"" . $sex . "\",\"age\":\"" . $age . "\",\"weight\":\"" . $weight . "\",\"height\":\"" . $height . "\"}";
}
$returnData .="]";
echo $returnData;
?>(7)删除指定的一条数据:
header('Access-Control-Allow-Origin:*');
$number = $_POST["number"];
$connect = mysql_connect("localhost","root","1214078831ljl");
if(!$connect){
die("database connect fail error = " . mysql_error());
}
mysql_select_db("revival",$connect);
//query number equal variable $number the data
$deleteSyntax = "DELETE FROM person WHERE number = \"" . $number ."\"";
echo $deleteSyntax;
mysql_query($deleteSyntax);
?>
修改数据这里就不写了,可以到w3c网去查。
这里有一个操作数据库的例子,有兴趣的朋友可以看看,基础版,大神止步。
http://pan.baidu.com/s/1qYe1e5e