Dalam bab ini, kita akan mempelajari package DBI untuk menyambungkan ke database dan kemudian mengambil data dengan query SQL. SQL (Structured Query Language), adalah bahasa pemrograman untuk database dan merupakan bahasa terpenting bagi semua jenis data analitik.
Pada contoh ini kita akan meng-import database SQL berisi data postingan dan pesan user Tweats. Pertama kita perlu me-load package DBI dan RMySQL, kemudian menghubungkan koneksi database tweater.
#> Load Paket DBI dan RMySQLlibrary(DBI)library(RMySQL)#> Koneksi databasecon <-dbConnect(RMySQL::MySQL(), dbname ="tweater", host ="courses.csrrinzqubik.us-east-1.rds.amazonaws.com", port =3306,user ="student",password ="datacamp")dbListTables(con)
[1] "comments" "tweats" "users"
1.1 Tampilkan Semua Tabel
Terlihat ada 3 tabel yaitu “comments”, “tweats”, dan “users”
tables <-dbListTables(con)table_names <-dbListTables(con)#> Import semua tabeltables <-lapply(table_names, dbReadTable, conn = con)#> Tampilkan semua Tabeltables
[[1]]
id tweat_id user_id message
1 1022 87 7 nice!
2 1000 77 7 great!
3 1011 49 5 love it
4 1012 87 1 awesome! thanks!
5 1010 88 6 yuck!
6 1026 77 4 not my thing!
7 1004 49 1 this is fabulous!
8 1030 75 6 so easy!
9 1025 88 2 oh yes
10 1007 49 3 serious?
11 1020 77 1 couldn't be better
12 1014 77 1 saved my day
[[2]]
id user_id
1 75 3
2 88 4
3 77 6
4 87 5
5 49 1
6 24 7
post
1 break egg. bake egg. eat egg.
2 wash strawberries. add ice. blend. enjoy.
3 2 slices of bread. add cheese. grill. heaven.
4 open and crush avocado. add shrimps. perfect starter.
5 nachos. add tomato sauce, minced meat and cheese. oven for 10 mins.
6 just eat an apple. simply and healthy.
date
1 2015-09-05
2 2015-09-14
3 2015-09-21
4 2015-09-22
5 2015-09-22
6 2015-09-24
[[3]]
id name login
1 1 elisabeth elismith
2 2 mike mikey
3 3 thea teatime
4 4 thomas tomatotom
5 5 oliver olivander
6 6 kate katebenn
7 7 anjali lianja
1.2 Tampilkan tabel khusus
Misal kita hanya akan menampilkan tabel users
#> import tabel users dari data tweaterusers <-dbReadTable(con, "users")#> Tampilkan tabelusers
id name login
1 1 elisabeth elismith
2 2 mike mikey
3 3 thea teatime
4 4 thomas tomatotom
5 5 oliver olivander
6 6 kate katebenn
7 7 anjali lianja
1.3 Read data dengan SELECT dan WHERE
Kita akan menampilkan data post terbaru dari tabel tweats pada tanggal lebih dari ‘2015-09-21’
#> menampilkan data post dari tabel tweats pada tanggal lebih dari '2015-09-21'latest <-dbGetQuery(con, "SELECT post FROM tweatsWHERE date > '2015-09-21'")#> Tampilkan datalatest
post
1 open and crush avocado. add shrimps. perfect starter.
2 nachos. add tomato sauce, minced meat and cheese. oven for 10 mins.
3 just eat an apple. simply and healthy.
1.4 Fungsi INNER JOIN
Misal kita akan menampilkan gabungan data dari 2 tabel berbeda berdasarkan foreign key/id
#> Tampilkan nama dari tabel users dan post dari tabel tweats berdasarkan user id dimana tanggal post lebih dari '2015-09-19'dbGetQuery(con, "SELECT name, post FROM users INNER JOIN tweats on users.id = user_id WHERE date > '2015-09-19'")
name post
1 elisabeth nachos. add tomato sauce, minced meat and cheese. oven for 10 mins.
2 oliver open and crush avocado. add shrimps. perfect starter.
3 kate 2 slices of bread. add cheese. grill. heaven.
4 anjali just eat an apple. simply and healthy.