Skip to content

Commit

Permalink
renaming connections in the db reading section
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorcampbell committed Aug 21, 2024
1 parent caa6104 commit 46383a7
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions source/reading.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ communication channel that R can use to send SQL commands to the database.
```{r}
library(DBI)
conn_lang_data <- dbConnect(RSQLite::SQLite(), "data/can_lang.db")
canlang_conn <- dbConnect(RSQLite::SQLite(), "data/can_lang.db")
```

Often relational databases have many tables; thus, in order to retrieve
Expand All @@ -557,7 +557,7 @@ all the tables in the database using the `dbListTables` \index{database!tables}
function:

```{r}
tables <- dbListTables(conn_lang_data)
tables <- dbListTables(canlang_conn)
tables
```

Expand All @@ -573,7 +573,7 @@ into SQL queries!
```{r}
library(dbplyr)
lang_db <- tbl(conn_lang_data, "lang")
lang_db <- tbl(canlang_conn, "lang")
lang_db
```

Expand All @@ -596,15 +596,15 @@ image_read("img/reading/ref_vs_tibble.001.jpeg") |>
```

We can look at the SQL commands that are sent to the database when we write
`tbl(conn_lang_data, "lang")` in R with the `show_query` function from the
`tbl(canlang_conn, "lang")` in R with the `show_query` function from the
`dbplyr` package. \index{database!show\_query}

```{r}
show_query(tbl(conn_lang_data, "lang"))
show_query(tbl(canlang_conn, "lang"))
```

The output above shows the SQL code that is sent to the database. When we
write `tbl(conn_lang_data, "lang")` in R, in the background, the function is
write `tbl(canlang_conn, "lang")` in R, in the background, the function is
translating the R code into SQL, sending that SQL to the database, and then translating the
response for us. So `dbplyr` does all the hard work of translating from R to SQL and back for us;
we can just stick with R!
Expand Down Expand Up @@ -704,7 +704,7 @@ be able to connect to a database using this information.

```{r, eval = FALSE}
library(RPostgres)
conn_mov_data <- dbConnect(RPostgres::Postgres(), dbname = "can_mov_db",
canmov_conn <- dbConnect(RPostgres::Postgres(), dbname = "can_mov_db",
host = "fakeserver.stat.ubc.ca", port = 5432,
user = "user0001", password = "abc123")
```
Expand All @@ -714,7 +714,7 @@ to when we were using an SQLite database in R. For example, we can again use
`dbListTables` to find out what tables are in the `can_mov_db` database:

```{r, eval = FALSE}
dbListTables(conn_mov_data)
dbListTables(canmov_conn)
```

```
Expand All @@ -727,7 +727,7 @@ We see that there are 10 tables in this database. Let's first look at the
database:

```{r, eval = FALSE}
ratings_db <- tbl(conn_mov_data, "ratings")
ratings_db <- tbl(canmov_conn, "ratings")
ratings_db
```

Expand Down

0 comments on commit 46383a7

Please sign in to comment.