我已经使用postgresql成功地连接到RStudio中的postgres数据库,并收回了必要的数据。没问题。
问题是,现在RStudio中有了数据集,我希望能够使用sqldf将其作为数据帧进行查询。这就是问题所在。
我已经试过以下代码

tab1 <-  DBI::dbGetQuery(con, "SELECT   a.user_id
                                     ,a.some_id1
                                     ,a.some_id2
                                     ,a.some_var1
                                     ,a.some_var2
                                     ,a.some_var3
                                     ,a.some_var4
                                     ,a.some_var5
                                     ,b.some_var6  FROM sessions a LEFT JOIN session_experiments b on a.some_id1 = b.some_id2
                                      AND a.some_var1 = b.some_var1")

同样,这会返回我想在RStudio中看到的数据。
然后我试着。。。
tab2 <- sqldf("SELECT COUNT (DISTINCT some_id1) FROM tab1")

…我看到下面的错误。
Error in postgresqlNewConnection(drv, ...) :
  RS-DBI driver: (could not connect postgres@localhost:5432 on dbname "test": could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
)
Error in !dbPreExists : invalid argument type

承认postgresql不是我以前用过的包,所以希望能得到一些帮助。
提前谢谢

最佳答案

好的,问题似乎在于当还原为sqldf时,需要显式地指定驱动程序和dbname,如下例所示:

sqldf(query, drv="SQLite", dbname=":memory:")

我不知道这个,但这解决了我的问题,所以我会考虑问题的答案。
请在此处阅读更多内容:
https://www.r-bloggers.com/using-postgresql-in-r-a-quick-how-to/

关于r - 如何将数据从Postgresql数据提取到数据框以与sqldf一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56444480/

10-09 05:59
查看更多