문제

Using RMySQL I want to load data from a database into a dataframe in R. For this I'm using the following code:

Rconnectdb:

con <- dbConnect(MySQL(),
user="root", password="password",
dbname="prediction", host="localhost")

Main code

library(RMySQL)
source("Rconnectdb") #load the database connection
query = "select received,isRefound from message" #specify query
rs=dbGetQuery(con,query) #resultset
dataset <- fetch(rs, n=-1) #fill dataset with all rows of the resultset
dbClearResult(rs) #clear resultset

Executing this I get the following error

Error in function (classes, fdef, mtable) : unable to find an inherited method for function "fetch", for signature "data.frame", "numeric"

Any ideas?

도움이 되었습니까?

해결책

You're mistaking dbSendQuery with dbGetQuery.
dbGetQuery combine dbSendQuery, fetch and dbClearResult as per documentation:

The function dbSendQuery only submits and synchronously executes the SQL statement to the database engine. It does not extracts any records — for that you need to use the function fetch (make sure you invoke dbClearResult when you finish fetching the records you need).

The function dbGetQuery does all these in one operation (submits the statement, fetches all output records, and clears the result set).

From ?dbGetQuery in package DBI.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top