forked from DarrellAucoin/IntroSQL
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUsing_SQL_with_R.Rmd
More file actions
158 lines (116 loc) · 3.08 KB
/
Using_SQL_with_R.Rmd
File metadata and controls
158 lines (116 loc) · 3.08 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
title: "Using SQL within R"
author: "Darrell Aucoin"
date: "February 13, 2015"
output: html_document
---
For SQL within R, we usually need two packages:
1. __DBI__: R Database Interface
2. Specific package for the individual SQL implementation (MySQL, SQLite, Oracle, etc.).
- Package `RSQLite` for SQLite
## DBI Functions
DBI contains various virtual classes and functions in connecting and querying a database:
__dbDriver__ Driver specifying the operations for creating connections to SQL Servers
For SQLite:
```r
m = dbDriver("SQLite") # equivalent to SQLite()
```
For MySQL:
```r
m = dbDriver("MySQL") # equivalent to MySQL()
```
__dbConnect__ Connect to a DBMS.
- For SQLite:
```r
conn = dbConnect(m, "path_to_file")
```
- For MySQL
```r
conn = dbConnect(m, user="user_name", db="database_name", host="SQL_server", password="password")
```
__dbDisconnect__ Disconnect from a DBMS. You should always disconnect after you no longer need it
```r
dbDisconnect(conn)
```
__dbSendQuery__ Submits and executes SQL statement (information retrieved using fetch)
```r
query = dbSendQuery(conn, "SELECT statement;")
```
__fetch__ Get records from a dbSendQuery
```r
res = fetch(query)
```
__dbClearResult__ _Free all resources_ (local and remote) from a query.
```r
dbClearResult(query)
```
__dbGetQuery__ Submits, executes SQL statement and retrieves information
```r
res = dbGetQuery(conn, "SELECT statement;")
```
__dbListTables__ List tables in database connection
```r
tables = dbListTables(conn)
```
__dbGetInfo__ Get meta-data for DBIObjects
```r
meta.data = dbGetInfo(query)
```
__dbReadTable__ Fetch the data from a table.
```r
res = dbReadTable(conn, "table_name")
```
__dbListFields__ Return the column names for a given table
```
columns = dbListFields(conn, "table_name")
```
## Connecting a SQLite Database
```{r}
library(RSQLite)
library(xtable)
m = dbDriver("SQLite")
con = dbConnect(m, "/Users/darrellaucoin/GitHub/IntroSQL/stats_club.db")
query = dbSendQuery(con,
"SELECT *
FROM exec;")
result = fetch(query)
result
dbClearResult(query)
dbDisconnect(con)
```
---
```{r}
library(RSQLite)
m = dbDriver("SQLite")
con = dbConnect(m, "/Users/darrellaucoin/GitHub/IntroSQL/stats_club.db")
result = dbGetQuery(con,
"SELECT name, start_time, location
FROM event;")
result
dbDisconnect(con)
```
## Connecting to Local File System using MySQL
I transferred the data in stats_club.db to MySQL on my laptop.
```{r}
library("RMySQL")
m = dbDriver("MySQL")
conn = dbConnect(m, user="darrell", db="stats_club",
host="localhost",
password="pass")
query = dbSendQuery(conn,
"SELECT name, start_time, location
FROM event")
result = fetch(query)
dbClearResult(query)
print(result)
dbDisconnect(conn)
```
## Connecting to Remote Server using MySQL
```{r}
hg19 = dbConnect(MySQL(), user="genome", db="hg19",
host="genome-mysql.cse.ucsc.edu")
result = dbGetQuery(hg19,
"SELECT COUNT(*) FROM affyU133Plus2")
dbDisconnect(hg19)
print(result)
```