我正在使用 PHP 将表从 Android(SQLite)导出到 MYSQL 以与服务器通信(我XAMPP
用作工具)。
我有一个sqlite
带有字符串字段(名为“开始”)的表格,它以我特别设计的格式告诉我当前时间。例如:0:03:14 -- 2013/8/12。我将此信息提供给HttpPost
在 Android 中使用的服务器。
问题来了。例如,我为“开始”字段使用“varchar(50)”类型为 mysql 创建表。当我插入表格时,我收到了使用 JSON 的下一条消息:
08-12 16:51:25.383: D/MYSQL(26304): jsonResult {"phpError":"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':36:12 -- 2013\/8\/6, \r\n\t18:36:17 -- 2013\/7\/6, 30, 57)' at line 2","Success":0,"Message":"Oops! Couldnt upload data","Start":"18:36:12 -- 2013\/8\/6"}
如您所见,原始起始字符串是 18:36:12,但是,在错误消息中它显示为“在 ':36:12 附近使用”,因此“解析”字符串 (?) 似乎有一些问题。有谁知道发生了什么?我应该为此字段使用不同的类型吗?
这是我的 PHP 文件:
<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$sql1 = "CREATE TABLE IF NOT EXISTS mobile_metadata(ID int, Acc int, Mag int, Gyr int, Temperature int, Grav int,
Light int, LinAcc int, Pressure int, Proximity int, Humidity int, RotVec int, Format varchar(50), Start varchar(50),
Finish varchar(50), First int, Last int);";
$result1 = mysql_query($sql1);
//Data succesfully created/was already created
if($result1){
$id = $_POST['id']; $acc = $_POST['Acc']; $mag = $_POST['Mag']; $gyr = $_POST['Gyr']; $temperature = $_POST['Temperature']; $grav = $_POST['Grav'];
$light = $_POST['Light']; $linAcc = $_POST['LinAcc']; $pressure = $_POST['Pressure']; $proximity = $_POST['Proximity']; $humidity = $_POST['Humidity'];
$rotVec = $_POST['RotVec']; $format = $_POST['Format']; $start = $_POST['Start']; $finish = $_POST['Finish']; $first = $_POST['First'];
$last = $_POST['Last'];
$sql2 = "INSERT into mobile_metadata(ID, Acc, Mag, Gyr, Temperature, Grav, LinAcc, Pressure, Proximity, Humidity, RotVec, Format, Start, Finish,
First, Last) VALUES ($id, $acc, $mag, $gyr, $temperature, $grav, $light, $linAcc, $pressure, $proximity, $humidity, $rotVec, $format, $start,
$finish, $first, $last);";
$result2 = mysql_query($sql2);
//Data uploaded
if($result2){
$response['Success'] = 1;
$response['Message'] = "Data uploaded succesfuly";
echo json_encode($response);
}
//Couldnt upload data
else{
$response["phpError"] = mysql_error();
$response['Success'] = 0;
$response['Message'] = "Oops! Couldnt upload data";
$response['Start'] = $_POST['Start'];
echo json_encode($response);
}
}
//Couldnt create the table
else{
$response["phpError"] = mysql_error();
$response["Success"] = 0;
$response["Message"] = "Oops! Couldnt create the table";
echo json_encode($response);
}
?>
谢谢。