目前,在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
我尝试了以下hack,这是之前类似问题的答案中提供的建议。这是链接... 如何在R中写入数据到PostgreSQL表格并使用自增主键?
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()
函数。这花费了一些时间(我想接近一个小时),但它确实起作用了。 - bgoldstinsert into
方法会失败...而即便是小量数据(1x1 df),dbWriteTable()
也会失败并提示错误。 - Gaurav