Failed to connect the database when using sqldf in r

victordongy picture victordongy · Jul 17, 2016 · Viewed 11.3k times · Source

I loaded a csv file to my R, and when I Tried to use sqldf to select some column, it always went to

Error in .local(drv, ...) : 
  Failed to connect to database: Error: Access denied for user 
  'User'@'localhost' (using password: NO)
Error in !dbPreExists : invalid argument type

I don't know how to fix it.

Here is my script:

library("RMySQL")
library(sqldf)
acs<-read.csv("getdata_data_ss06pid.csv",head = T)
sqldf("select pwgtp1 from acs where AGEP < 50")

Answer

Psidom picture Psidom · Jul 17, 2016

It doesn't seem like you need to load the RMySQL library when using sqldf, since you have already read the data into memory, which seems to be the problem here:

library(RMySQL)
library(sqldf)
sqldf("select * from df limit 6")

Error in .local(drv, ...) : Failed to connect to database: Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) Error in !dbPreExists : invalid argument type

However if RMySQL library is detached, sqldf works:

detach("package:RMySQL", unload=TRUE)
sqldf("select * from df limit 6")
#   time  type
# 1    1 type1
# 2    2 type1
# 3    3 type2
# 4    4 type1
# 5    5 type2
# 6    6 type1