我编写了一个脚本,允许客户端上传CSV数据文件,然后将其输入数据库。脚本首先将CSV文件上载到服务器,然后脚本的第二部分读取CSV文件,然后将该信息输入到数据库中。
但是,当CSV数据插入数据库时,我收到以下错误:
SQLSTATE [42000]:语法错误或访问冲突:1064您有 SQL语法错误;查看与您的手册相对应的手册 MariaDB服务器版本用于正确的语法使用附近 '英国,管理,公司英国格拉斯哥,格拉斯哥,G1 G1 1AA,3723,已完成,已安排,2014-03'在第1行
以下是CSV数据:
account_number,customer_reference,billing_customer,division,customer_site,town,postcode,job_number,job_status,job_type,completion_date,description,waste_type,ewc_code,container,quantity,total_uom,co2_saving,total_resource,total_co2_saving,recovery_rate,disposal_method,waste_hierarchy,customer_order_date,job_notes,customer_ref_po,uploaded,processed,deleted
1,633,Company UK,Managed,Company UK Glasgow,Glasgow,G1 G1 1AA,3723,Completed,Scheduled,2014-03-24,24/05/2014 - General Waste,General Waste to Energy,20 03 01,Wheelie Bin 1100 Litre,4,320,0.84,0,0.03,0,Transfer Station,Incinerator,2014-05-27,,0,2014-08-05,1,0
这是带有上传和CSV插入脚本的Controller: $ fileUpload = $ this-> createForm(new FileuploadType());
$request = $this->getRequest();
if($request->getMethod() == 'POST') {
$fileUpload->bind($request);
$dir = '/var/www/cwwa/web/uploads/csv/';
if($fileUpload->isValid()) {
$file2 = $fileUpload['attachment']->getData();
$file2->move($dir, $file2->getClientOriginalName());
$fileName = $file2->getClientOriginalName();
$uploadedFile = basename($dir.''.$file2->getClientOriginalName());
$recordData = array();
$table = 'enviro_figures_upload';
if (($file = fopen($dir.$fileName, "r")) !== FALSE) {
$row = 0;
while (($data = fgetcsv($file, 1000, ",")) !== FALSE) {
if ($row == 0) {
$fieldNames = $data;
$row++;
} else {
$recordData[] = $data;
$row++;
}
}
}
$fields = implode(',', $fieldNames);
foreach ($recordData as $record) {
$values = implode(',',$record);
$sql = "INSERT INTO $table ($fields) VALUES ($values);";
}
$rsm = new ResultSetMapping($dm);
$insert = $dm->createNativeQuery($sql, $rsm);
$result = $insert->getResult();
}
}
我已经为数据添加了引号,但错误仍然存在。
修改
我改变了这一行:
$values = implode(',',$record);
对此:
$values = '"'.implode(',',$record).'"';
这会为添加的值添加引号,但这会在值字符串的任一侧产生引号。见下文:
An exception occurred while executing 'INSERT INTO enviro_figures_upload (account_number,customer_reference,billing_customer,division,customer_site,town,postcode,job_number,job_status,job_type,completion_date,description,waste_type,ewc_code,container,quantity,total_uom,co2_saving,total_resource,total_co2_saving,recovery_rate,disposal_method,waste_hierarchy,customer_order_date,job_notes,customer_ref_po,uploaded,processed,deleted) VALUES (1,633,Company UK,Managed,Company UK Glasgow,Glasgow,G1 G1 1AA,3723,Completed,Scheduled,2014-03-24,24/05/2014 - General Waste,General Waste to Energy,20 03 01,Wheelie Bin 1100 Litre,4,320,0.84,0,0.03,0,Transfer Station,Incinerator,2014-05-27,,0,2014-08-05,1,0);':
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UK,Managed,Company UK Glasgow,Glasgow,G1 G1 1AA,3723,Completed,Scheduled,2014-03' at line 1
编辑2
根据VMai的评论提出了这一点:
$values = '"' . implode('","',$record) . '"';
这解决了SQLSTATE问题。但是,它引发了一个新问题:
SQLSTATE [HY000]:常规错误
我已经检查了数据库,并且无论CSV文件中有多少行,Query都会插入一行数据。刷新页面然后将同一行数据插入数据库。
答案 0 :(得分:0)
您必须为每条记录执行INSERT:
所以它应该是
// I assume we need only one of this
$rsm = new ResultSetMapping($dm);
foreach ($recordData as $record) {
$values = '"'. implode('","',$record) . '"';
$sql = "INSERT INTO $table ($fields) VALUES ($values);";
$insert = $dm->createNativeQuery($sql, $rsm);
$result = $insert->getResult();
}
代替。
<强>备注强>
我建议使用准备好的声明。在循环之前准备一次,并在每个记录的循环内绑定输入值来执行它。
答案 1 :(得分:-2)
Symfony 2.3
我建议使用object(flush),但是如果你使用没有实体的sql,试试这个
foreach ($recordData as $record) {
$values = '"'. implode('","',$record) . '"';
$sql = "INSERT INTO $table ($fields) VALUES ($values);";
$em = $this->getDoctrine()->getManager()->getConnection()->prepare($sql);
$em->execute();
}