Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby CSV文件有问题。“处或附近出现语法错误”&引用;(PG::SyntaxError)_Ruby_Database_Csv - Fatal编程技术网

Ruby CSV文件有问题。“处或附近出现语法错误”&引用;(PG::SyntaxError)

Ruby CSV文件有问题。“处或附近出现语法错误”&引用;(PG::SyntaxError),ruby,database,csv,Ruby,Database,Csv,我有一个CSV文件,其中有很多行。我会显示第一行,因为这是问题的起点。所以我的任务是将数据从CSV文件插入数据库 phone_number,start_time,session_time,client_status,operator_status,custom_status,sip and so on... 42424242, 2019-12-09 14:01:17, , CANCEL, , , , 1, 0, some name, 2222222, info, info, 242424, ,

我有一个CSV文件,其中有很多行。我会显示第一行,因为这是问题的起点。所以我的任务是将数据从CSV文件插入数据库

phone_number,start_time,session_time,client_status,operator_status,custom_status,sip and so on...
42424242, 2019-12-09 14:01:17, , CANCEL, , , , 1, 0, some name, 2222222, info, info, 242424, , 0, 
这是我的ruby脚本

require 'csv'
require 'pg'

host = "localhost"
port = 5432
database = "testdb"
username = "XXX"
password = "XXX"

pg_conn = PG.connect(host,port,"","",database,username,password)
puts pg_conn

csv_text = File.read('/home/XXX/XXX/XXX/calls.csv')
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1', :col_sep => ',')

csv.each do |row|
  pg_conn.exec("INSERT INTO testing(custom_field_2, phone_number, client_status, operator_status, start_time, sip)
                VALUES (#{row['custom_field_2']},#{row['phone_number']},#{row['client_status']},#{row['operator_status']},#{row['start_time']},#{row['sip']})")
end
当我试图运行脚本时,我得到了这个错误

script.rb:24:in `exec': ERROR:  syntax error at or near "," (PG::SyntaxError)
LINE 2: ...              VALUES (2222222,42424242,CANCEL,,2019-12-0...
                                                         ^
所以问题从csv.each开始。我尝试从csv.each中删除“客户端状态”,然后它就工作了

#{row['client_status']}
因为在csv文件中,一些“client_status”行是空的,我不知道如何让ruby跳过空位,或者以某种方式计算空位,但使用引号。当我运行脚本时,也许可以将每一行都用引号引起来。目前,我的纸条认为我想插入一个逗号,但那只是一个分隔符

"42424242", "2019-12-09 14:01:17","  ", "CANCEL"," "," "," ", "1", "0", "some name", "2222222", "info", "info", "242424"," ", "0", 
编辑: 问题不在于csv文件或pg_conn.exec 实际上是我的sql函数出错了。但是被接受的答案也解决了我的问题,而我不需要更改sql函数

我不知道如何让ruby跳过空白点,或者以某种方式计算空白点,但是要用引号

PG gem提供了一种将Ruby对象转换为有效SQL类型的方法,例如将空字符串转换为
nil
转换为
NULL

exec_params
接受两个参数:SQL查询作为字符串,Ruby对象作为数组。在SQL查询中,可以使用
$1
$2
$3
等(基于1)引用数组中的对象(基于0)

像这样的方法应该会奏效:

csv.each do |row|
  values = row.fields('custom_field_2', 'phone_number', 'client_status', 'operator_status', 'start_time', 'sip')

  pg_conn.exec_params(<<-SQL, values)
    INSERT INTO testing (custom_field_2, phone_number, client_status, operator_status, start_time, sip)
      VALUES ($1, $2, $3, $4, $5, $6);
  SQL
end
csv.do每行|
值=行字段('custom_field_2','phone_number','client_status','operator_status','start_time','sip')
pg_conn.exec_参数(
我不知道如何让ruby跳过空白点,或者以某种方式计算空白点,但是要用引号

PG gem提供了一种将Ruby对象转换为有效SQL类型的方法,例如将空字符串转换为
nil
转换为
NULL

exec_params
接受两个参数:SQL查询作为字符串,Ruby对象作为数组。在SQL查询中,可以使用
$1
$2
$3
等(基于1)引用数组中的对象(基于0)

像这样的方法应该会奏效:

csv.each do |row|
  values = row.fields('custom_field_2', 'phone_number', 'client_status', 'operator_status', 'start_time', 'sip')

  pg_conn.exec_params(<<-SQL, values)
    INSERT INTO testing (custom_field_2, phone_number, client_status, operator_status, start_time, sip)
      VALUES ($1, $2, $3, $4, $5, $6);
  SQL
end
csv.do每行|
值=行字段('custom_field_2','phone_number','client_status','operator_status','start_time','sip')

pg_conn.exec_params(尝试通过
pg::Connection#exec_params
使用参数化查询,而不是使用字符串插值的
pg::Connection#exec
——前者应该“清理”正确使用空白值。尝试使用参数化查询,通过<代码> PG::CONNEXY Excel PARAMS < /代码>,而不是<代码> PG::连接字符串*Exc< /COD>字符串插值-前者应该正确地“清除”空白值。