【发布时间】:2016-06-06 07:11:33
【问题描述】:
目前要在 PostgreSQL 表中插入数据,我必须创建一个空表,然后执行 insert into table values ... 以及折叠成包含所有值的单个字符串的数据框。它不适用于大型数据框。
dbWtriteTable() 不适用于 PostgreSQL,并给出以下错误...
Error in postgresqlpqExec(new.con, sql4) : RS-DBI driver: (could not Retrieve the result : ERROR: syntax error at or near "STDIN" LINE 1: COPY "table_1" FROM STDIN
我已经按照之前提出的类似问题的建议尝试了以下技巧。这是链接...How do I write data from R to PostgreSQL tables with an autoincrementing primary key?
body_lines <- deparse(body(RPostgreSQL::postgresqlWriteTable))
new_body_lines <- sub(
'postgresqlTableRef(name), "FROM STDIN")',
'postgresqlTableRef(name), "(", paste(shQuote(names(value)), collapse = ","), ") FROM STDIN")',
body_lines,
fixed = TRUE
)
fn <- RPostgreSQL::postgresqlWriteTable
body(fn) <- parse(text = new_body_lines)
while("RPostgreSQL" %in% search()) detach("package:RPostgreSQL")
assignInNamespace("postgresqlWriteTable", fn, "RPostgreSQL")
这个技巧仍然对我不起作用。 postgresqlWriteTable() 抛出完全相同的错误......
这到底是什么问题?
作为替代方案,我尝试使用来自caroline 包的dbWriteTable2()。它会引发不同的错误...
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR: column "id" does not exist in table_1
)
creating NAs/NULLs for for fields of table that are missing in your df
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR: column "id" does not exist in table_1
)
有没有其他方法可以直接将大数据框写入PostgreSQL中的表?
【问题讨论】:
-
我们在这里讨论的规模有多大?我刚刚使用
RPostgresQL::dbWriteTable()成功地将 R 中的 ~800MB 10,000x1,000 data.frame 写入 PostgreSQL 表。花了一段时间(我想大约是一个小时),但它奏效了。 -
@bgoldst 我的意思是
insert into方法对于大数据失败...dbWriteTable()即使对于小数据 (1X1 df) 也失败并给出错误说明
标签: r postgresql