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
Using sqldf and RPostgreSQL together
提问by djq
When using RPostgreSQL
I find that I cannot use sqldf
in 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 sqldf
I 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 sqldf
so 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
sqldf
will automatically work with the test
database in PostgreSQL if it sees that RPostgreSQL is loaded. So you can create a test
database in PostgreSQL and then use sqldf with that
sqldf
test
如果它看到 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 drv
argument 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 detach
RPostgreSQL before you use sqldf and load it again afterwards.
或者,如果您希望使用 RSQLite,另一种可能性是detach
在使用 sqldf 之前使用 RPostgreSQL,然后再次加载它。
See ?sqldf
for 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)