-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_from_mysql.r
57 lines (42 loc) · 1.55 KB
/
data_from_mysql.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
if (!require(RMySQL)) install.packages("RMySQL")
library(RMySQL)
ucscDb <- dbConnect(MySQL(), user = "genome",
# password="password",
host = "genome-mysql.cse.ucsc.edu")
result <- dbGetQuery(ucscDb, "show databases;")
dbDisconnect(ucscDb)
result
con <- dbConnect(RMySQL::MySQL(),
dbname="company",
host="courses.csrrinzqubik.us-east-1.rds.amazonaws.com",
port=3306,
user="student",
password="datacamp")
dbListTables(con)
employee.table <- dbReadTable(con,"employees")
str(employee.table)
print(con) # <MySQLConnection:0,1>
dbGetQuery(con, "select name from employees where started_at > \"2012-09-01\"")
## name
## 1 Julie
## 2 Heather
## 3 John
dbGetQuery(con, "select * from products where contract = 1")
## id name contract
## 1 2 Call Plus 1
## 2 9 Biz Unlimited 1
res <- dbSendQuery(con, "select * from products where contract = 1")
str(res) # Formal class 'MySQLResult' [package "RMySQL"] with 1 slot
dbFetch(res)
dbClearResult(res)
dbDisconnect(con)
## another one:
con <- dbConnect(RMySQL::MySQL(),
dbname = "tweater",
host = "courses.csrrinzqubik.us-east-1.rds.amazonaws.com",
port = 3306,
user = "student",
password = "datacamp")
long_tweats <- dbGetQuery(con, "select post,date from tweats where char_length(post) > 40")
print(long_tweats)
dbDisconnect(con)