如何从R将一行数据写入postgresql表?

如何从R将一行数据写入postgresql表?,r,postgresql,rpostgresql,R,Postgresql,Rpostgresql,我在postgresql数据库中有一个表myschema.fruits。在R脚本中,我希望在脚本末尾向该表插入一行。表格行有3列类型、口味和颜色。我在R脚本中的3个不同变量中有相同的变量名,如下所示: type <- "Apple" taste <- "Sweet" color <- "Red" type如有必要,请更改主机、端口、用户并添加密码 第一个选项:将数据框追加到表中 dt2insert = data.frame(type = "Apple",

我在postgresql数据库中有一个表
myschema.fruits
。在R脚本中,我希望在脚本末尾向该表插入一行。表格行有3列
类型
口味
颜色
。我在R脚本中的3个不同变量中有相同的变量名,如下所示:

type <- "Apple"
taste <- "Sweet"
color <- "Red"

type如有必要,请更改主机、端口、用户并添加密码

第一个选项:将数据框追加到表中

dt2insert = data.frame(type = "Apple",
                       taste = "Sweet",
                       color = "Red",
                       stringsAsFactors = FALSE)
con = dbConnect(dbDriver("PostgreSQL"),dbname = "mydatabase",
                host = "localhost", port = 5432,
                user = "postgres") 
dbWriteTable(con, name = c("myschema","fruits"), value = dt2insert,append=TRUE,row.names=FALSE,overwrite=FALSE)
dbDisconnect(con)
第二个选项:使用INSERT INTO命令

type <- "Apple"
taste <- "Sweet"
color <- "Red"
qry = paste0("INSERT INTO myschema.fruits VALUES ('",type,"','",taste,"','",color,"');")

con = dbConnect(dbDriver("PostgreSQL"),dbname = "mydatabase",
                host = "localhost", port = 5432,
                user = "postgres") 
dbSendQuery(con,qry)
dbDisconnect(con)

type如有必要,请更改主机、端口、用户并添加密码

第一个选项:将数据框追加到表中

dt2insert = data.frame(type = "Apple",
                       taste = "Sweet",
                       color = "Red",
                       stringsAsFactors = FALSE)
con = dbConnect(dbDriver("PostgreSQL"),dbname = "mydatabase",
                host = "localhost", port = 5432,
                user = "postgres") 
dbWriteTable(con, name = c("myschema","fruits"), value = dt2insert,append=TRUE,row.names=FALSE,overwrite=FALSE)
dbDisconnect(con)
第二个选项:使用INSERT INTO命令

type <- "Apple"
taste <- "Sweet"
color <- "Red"
qry = paste0("INSERT INTO myschema.fruits VALUES ('",type,"','",taste,"','",color,"');")

con = dbConnect(dbDriver("PostgreSQL"),dbname = "mydatabase",
                host = "localhost", port = 5432,
                user = "postgres") 
dbSendQuery(con,qry)
dbDisconnect(con)

<代码>类型< P>作为一种替代方法,使用<强>插入到命令中,考虑使用低级<代码> PoxGrqLExpReals< /Cuff>函数,这允许查询的参数化。这样做的主要优点是,您不必为适当的数据类型手动构造查询字符串,在这种情况下,您可以省去额外的引号
'

type <- "Apple"
taste <- "Sweet"
color <- "Red"

con = dbConnect(dbDriver("PostgreSQL"),dbname = "mydatabase",
                host = "localhost", port = 5432,
                user = "postgres") 
tmp <- postgresqlExecStatement(con,
               'insert into myschema.fruits VALUES ($1, $2, $3)',
               list(type, taste, color))
dbClearResult(tmp)
dbDisconnect(con)

<代码>类型< P>作为一种替代方法,使用<强>插入到命令中,考虑使用低级<代码> PoxGrqLExpReals< /Cuff>函数,这允许查询的参数化。这样做的主要优点是,您不必为适当的数据类型手动构造查询字符串,在这种情况下,您可以省去额外的引号
'

type <- "Apple"
taste <- "Sweet"
color <- "Red"

con = dbConnect(dbDriver("PostgreSQL"),dbname = "mydatabase",
                host = "localhost", port = 5432,
                user = "postgres") 
tmp <- postgresqlExecStatement(con,
               'insert into myschema.fruits VALUES ($1, $2, $3)',
               list(type, taste, color))
dbClearResult(tmp)
dbDisconnect(con)
类型