---
title: "Lesson 70.110 Starter"
subtitle: "Connect to Database"
author: Martin Schedlbauer, PhD
output: html_notebook
---

Connect to database

```{r}
library(RSQLite)

# be sure path ends in /
fpath = ""
dbfile = "orgdb.db"

dbcon <- dbConnect(RSQLite::SQLite(), paste0(fpath,dbfile))
```

Turn on support for foreign key constraint checking

```{sql connection=dbcon}
PRAGMA foreign_keys = ON
```

A simple query from a {sql} chunk.

```{sql connection=dbcon}
select * from Worker
```

```{sql connection=dbcon, output.var="workers.df"}
select * from Worker
```

```{r}
print(workers.df)
```

Embedded SQL query.

```{r}
sqlcmd <- "select * from Worker"
workers.df <- dbGetQuery(con = dbcon, sqlcmd)
print(workers.df)
```

List meta data.

```{r}
dbListTables(dbcon)
dbListFields(dbcon, "Worker")
```

Disconnect from database.

```{r}
dbDisconnect(dbcon)
```
