-->

dplyr & monetdb - appropriate syntax for querying

2019-08-11 17:20发布

问题:

In monetdb I have set up a schema main and my tables are created into this schema.

For example, the department table is main.department.

With dplyr I try to query the table:

mdb <- src_monetdb(dbname="model", user="monetdb", password="monetdb")

tbl(mdb, "department")

But I get

Error in .local(conn, statement, ...) : 
  Unable to execute statement 'PREPARE SELECT * FROM "department"'.
Server says 'SELECT: no such table 'department'' [#42S02].

I tried to use "main.department" and other similar combinations with no luck.

What is the appropriate syntax?

回答1:

There is a somewhat hacky workaround for this: We can manually set the default schema for the connection. I have a database testing, in there is a schema foo with a table called bar.

mdb <- src_monetdb("testing")
dbSendQuery(mdb$con, "SET SCHEMA foo");
t <- tbl(mdb, "bar")


回答2:

The dbplyr package (a backend of dplyr for database connections) has a in_schema() function for these cases:

conn <- dbConnect(
  MonetDB.R(),
  host = "localhost",
  dbname = "model",
  user = "monetdb",
  password = "monetdb",
  timeout = 86400L
)

department = tbl(conn, dbplyr::in_schema("main", "department"))


标签: r dplyr monetdb