knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(DBI) library(DBIExt) config <- list( HOST = "localhost", DATABASE = "mktdata000", USER = "test", PASSWORD = "test"); #' get the main database connection getDBConnection <- function(){ connection <- DBI::dbConnect(RMariaDB::MariaDB(), host = config[["HOST"]], user = config[["USER"]], password = config[["PASSWORD"]], dbname = config[["DATABASE"]]) } #' close the database connection closeDBConnection <- function(connection) { DBI::dbDisconnect(connection) } db <- getDBConnection(); db
cid <- 1:26; cn <- 100:125; clc <- letters[cid]; target.df <- tibble::as_tibble(list(id = cid, cn = cn, cc = clc)); DBI::dbCreateTable(db,"T123",target.df); dbAddPrimaryKey(db,"T123",primary_key = c("id")) DBI::dbAppendTable(db,"T123",target.df) original <- dbReadTable(db, "T123") original
cuc <- LETTERS[cid]; update.df <- tibble::as_tibble(list(id = cid, cc = cuc)); update.df <- dplyr::group_by(update.df,id) update.df
res <- dbUpdateTable(db,"T123",update.df); print(glue::glue("number of records modified in DBMS {res}")) updated <- dbReadTable(db,"T123"); closeDBConnection(db); updated
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.