简体   繁体   English

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

[英]How do I write a single row of data into a postgresql table from R?

I have a table myschema.fruits in a postgresql database mydatabase . 我在postgresql数据库mydatabase有一个表myschema.fruits From within an R script I would like to insert a single row to that table, at the end os my script. 从R脚本中我想在该表中插入一行,最后是我的脚本。 The table row has 3 columns type , taste and color . 表格行有3列typetastecolor Those I have in 3 different variables in my R script with same variable names like so: 我在R脚本中的3个不同变量中具有相同变量名称的变量如下:

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

I would like to use the RPostgreSQL driver to perform this insert, but I can't figure out how to do it? 我想使用RPostgreSQL驱动程序来执行此插入,但我无法弄清楚如何做到这一点?

As an alternative method using the INSERT INTO command, consider using the low level postgresqlExecStatement function, which allows parametrization of the query. 作为使用INSERT INTO命令的替代方法,请考虑使用低级postgresqlExecStatement函数,该函数允许查询的参数化。 The primary advantage of this is that you don't have to manually construct the query string for the appropriate data types, in this case you can leave out the extra quotation marks ' : 这样做的主要优点是您不必手动构造适当数据类型的查询字符串,在这种情况下,您可以省略额外的引号'

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)

Please change host, port, user and add password if necessary. 如有必要,请更改主机,端口,用户并添加密码。

First option: appending a data frame to the table 第一个选项:将数据框附加到表中

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)

Second option: using INSERT INTO command 第二个选项:使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用自动增量主键将数据从R写入PostgreSQL表? - How do I write data from R to PostgreSQL tables with an autoincrementing primary key? 如何从 R 在 PostgreSQL 中写表? - How to write a table in PostgreSQL from R? 根据行 ID 将 R 中的值写入 PostgreSQL 表 - Write values from R to a PostgreSQL table based on Row IDs 如何创建一个表,其中第一个表中的行也匹配 R 中第二个表的行中的 3 列 - How do I create a table with rows from a first table that also matches 3 columns in the row of a second table in R 如何在 R 的数据框中找出逗号在一行中出现的最大数量? - How do I find out the highest number that commas had appeared in a row in a single column in a data frame in R? 将每个提取的注释分配给单行write.table R数据帧 - Assign each extracted comment to a single row write.table R data frame 如何告诉write.table将每个列表项的数据保存在单独的行中? - How do I tell write.table to save the data of each list item in a separate row? 使用R中的data.table,如何有效替换单个列中命名的多个列值? - With data.table in R, how do I efficiently replace multiple column values named within a single column? R - 如何从数据框中的单列和单行获取字符串 - R - How to get at a string from a single column and row in a data frame 如何在R中对数据表行进行子集化以获取自身唯一的行 - How do I subset a data table row in R to get the rows unique to itself
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM