首页 > 解决方案 > SQLDF 在来自 R 的 PostgreSQL 查询上失败

问题描述

PostgreSQL 版本:11

R版本:3.5.2

操作系统:Windows 10

驱动程序:PostgreSQL ANSI(x64)

library(RODBC)
library(sqldf)
conn = odbcConnect("DVDRental") #works
odbcDataSources() #works
sqlTables(conn) #works
customers = sqldf(connection = conn, "select * FROM customer") #fails

错误消息:(函数(类,fdef,mtable)中的错误:无法找到签名“字符”,“字符”的函数“dbGetQuery”的继承方法</p>

有没有更新的方法将 R 连接到 Postgres?谷歌搜索发现了一大堆代码,这些代码似乎都不起作用。

如果你想尝试运行它,你可以下载我在这里使用的示例数据库:http ://www.postgresqltutorial.com/postgresql-sample-database/

我正在使用一个名为 DVDRental 的 DSN。

标签: rdatabasepostgresqlodbc

解决方案


使用RPostgreSQL库获取连接,使用选项:

library(RPostgreSQL)

options(sqldf.RPostgreSQL.user ="postgres", 
    sqldf.RPostgreSQL.password ="password",
    sqldf.RPostgreSQL.dbname ="test",
    sqldf.RPostgreSQL.host ="localhost", 
    sqldf.RPostgreSQL.port =5432)

customers = sqldf("SELECT * FROM customer")

推荐阅读