Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top