Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to connect the database when using sqldf in r

Tags:

r

sqldf

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")
like image 651
victordongy Avatar asked Jul 17 '16 00:07

victordongy


Video Answer


2 Answers

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
like image 168
Psidom Avatar answered Sep 30 '22 13:09

Psidom


TL;DR

Instead of unlaoding the RMySql package, explicitly set the sqldf default driver option to SQLite before calling the sqldf function:

options(sqldf.driver = "SQLite")
sqldf("select * from df limit 6")

Explanation

If not explicitly defined, the sqldf package decides which DB driver to use as follows:

If not specified then the "dbDriver" option is checked and if that is not set then sqldf checks whether RPostgreSQL, RMySQL or RH2 is loaded in that order and the driver corresponding to the first one found is used. If none are loaded then "SQLite" is used. dbname=NULL causes the default to be used.

In your case, RMySql has already been loaded and sqldf will try to use the MySQL DB and write into a schema called test. Detaching and unloading the RMySQL package is one option, but not necessary. As mentioned by @GaborGrothendieck in his comment, the easiest fix is to simply tell sqldf which DB driver to use explicitly, i.e.

sqldf("select * from df limit 6", drv="SQLite")

To not always having to add drv="SQLite", you can permanently set the default driver for the session to SQLite:

options(sqldf.driver = "SQLite")
like image 20
Mark Heckmann Avatar answered Sep 30 '22 14:09

Mark Heckmann