lundi 4 décembre 2017

R and MySQL: Pull Observations from MySQL Database using R

I would like to pull 10 observations from a MySQL database using R and the package "RMySQL". Here's my code so far using the iris data as an example:

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

rs <- dbSendQuery(con, "SELECT * FROM iris;")
iris <- fetch(rs, n=150)
check <- dbHasCompleted(rs)
dbClearResult(rs)
dbDisconnect(con)

head(iris)
  ID  Species Length_s Length_p Width_s Width_p
1  1 setosa\r      5.1      1.4     3.5     0.2
2  2 setosa\r      4.9      1.4     3.0     0.2
3  3 setosa\r      4.7      1.3     3.2     0.2
4  4 setosa\r      4.6      1.5     3.1     0.2
5  5 setosa\r      5.0      1.4     3.6     0.2
6  6 setosa\r      5.4      1.7     3.9     0.4

obs
 [1] 135  40  56  85 133  30 130 136  94  89

So now I want the code that will select the observations saved in "obs" in a mysql query that will pull only them from the database. Can I somehow invoke it using a command like this?

SELECT * FROM iris WHERE ID=obs;




Aucun commentaire:

Enregistrer un commentaire