postgresql 结合使用 sqldf 和 RPostgreSQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10237113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:50:11  来源:igfitidea点击:

Using sqldf and RPostgreSQL together

sqlrpostgresqlsqldfrpostgresql

提问by djq

When using RPostgreSQLI find that I cannot use sqldfin the same way. For example if I load the library and read in data into a data frame using the following code:

使用时RPostgreSQL我发现我不能sqldf以同样的方式使用。例如,如果我使用以下代码加载库并将数据读入数据帧:

library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host="localhost", user="postgres", password="xxx", dbname="yyy", port="5436")
rs <- dbSendQuery(con, "select * from table");                           
df<- fetch(rs, n = -1); dbClearResult(rs) 
dbDisconnect(con) 

I know have the contents of this table in the dataframe df. However if I want to run a SQL command using sqldfI would previously do something like this:

我知道数据框中有这个表的内容df。但是,如果我想使用 SQL 命令运行sqldf我以前会做这样的事情:

sqldf("SELECT * FROM df WHERE X > 10")

This no longer works as I get the error message:

当我收到错误消息时,这不再起作用:

Error in postgresqlNewConnection(drv, ...) : 
  RS-DBI driver: (could not connect postgres@localhost on dbname "test"
)
Error in !dbPreExists : invalid argument type

I assume this is operator error on my part, but I can't figure how what arguments to supply to sqldfso that it just focuses on the data frame and does not try to connect to anything else.

我认为这是我的操作员错误,但我无法弄清楚要提供什么参数,sqldf以便它只关注数据帧而不尝试连接到其他任何东西。

回答by G. Grothendieck

Using sqldf with RPostgreSQL

在 RPostgreSQL 中使用 sqldf

sqldfwill automatically work with the testdatabase in PostgreSQL if it sees that RPostgreSQL is loaded. So you can create a testdatabase in PostgreSQL and then use sqldf with that

sqldftest如果它看到 RPostgreSQL 已加载,它将自动使用PostgreSQL 中的数据库。所以你可以test在 PostgreSQL 中创建一个数据库,然后使用 sqldf

or, you can specify the name of a different database.

或者,您可以指定不同数据库的名称。

See: sqldf FAQ 12

请参阅:sqldf 常见问题 12

Using sqldf with RSQLite

在 RSQLite 中使用 sqldf

If you want to use sqldf with RSQLite rather than with RPostgreSQL you can use sqldf's drvargument to force it use a non-default driver. e.g.

如果您想将 sqldf 与 RSQLite 而不是 RPostgreSQL 一起使用,您可以使用sqldf'sdrv参数强制它使用非默认驱动程序。例如

sqldf("select foo from bar...",drv="SQLite")

or, you can set the driver globally using the "sqldf.driver"option. From within R:

或者,您可以使用该"sqldf.driver"选项全局设置驱动程序。从 R 中:

options(sqldf.driver = "SQLite")

or, another possibility if you wish to use RSQLite is to detachRPostgreSQL before you use sqldf and load it again afterwards.

或者,如果您希望使用 RSQLite,另一种可能性是detach在使用 sqldf 之前使用 RPostgreSQL,然后再次加载它。

See ?sqldffor details.

详情请参阅?sqldf

回答by Kevin Ogoro

I had the same error and I detached the RPostgeSQL package, rerun my sqldf code and it worked fine r detach("package:RPostgreSQL", unload=TRUE)

我有同样的错误,我分离了 RPostgeSQL 包,重新运行我的 sqldf 代码,它工作正常r detach("package:RPostgreSQL", unload=TRUE)