Connect to a local database from phpmyadmin with R

2019-05-30 02:34发布

I have searched for a lot of threads explaining the connexion with a database with R but I still can't make it work.

At the moment, I know that I have to install the package 'ROBDC' and use either odbcConnect() or odbcDriverConnect(). But the first one seems harder since I have to set an OBDC connection (tried some things but didn't work too t.t).

My final goal is to extract some data in a database located in a web server but, first, I wanted to see how the functions work for a local database created with Wamp Server. I'm currently working on Windows 7.

My database's name is 'extraction' and the id is 'root' (no pwd, usually) so I wrote :

install.packages('RODBC',repos="http://cran.rstudio.com/")

odbcDriverConnect(Driver='mysql'; Server=localhost;
Database=extraction; Uid='root'; pwd='')

I don't know if the syntax is correct, I have tried a lot and always the same error with drivers and data source unobtainable.

Here the full message (in French sorry D:)


"[RODBC] ERROR: state IM002, code 0, message [Microsoft][Gestionnaire de pilotes ODBC] Source de données introuvable et nom de pilote non spécifié"


I used this function to avoid the dns thing required for obdcConnect() but still I don't know if I'm doing the right thing (obvsly not since he doesn't work :D) but it's my first time trying with R to interact with databases so I don't know where to go even with all the help I have looked for.

Thanks in advance.

1条回答
小情绪 Triste *
2楼-- · 2019-05-30 03:25

Well, I found a solution with a package called RMySQL

install.packages('RMySQL')
require(RMySQL) #if already installed
con <- dbConnect(RMySQL::MySQL(), host = "localhost",dbname="extraction",user = "root", password = "")
test_extraction <- dbReadTable(con, "utilisateurs") #utilisateurs is a table from my database called extraction
查看更多
登录 后发表回答