Skip to content

Instantly share code, notes, and snippets.

@kilfu0701
Created October 28, 2013 07:49
Show Gist options
  • Save kilfu0701/7192862 to your computer and use it in GitHub Desktop.
Save kilfu0701/7192862 to your computer and use it in GitHub Desktop.
R mysql connect testing...
## base.R
library(DBI)
library(RMySQL)
DB_CONFIG <- list(
local = list(driver = 'MySQL', user = 'root', password = '', host = 'localhost'),
production = list(driver = 'MySQL', user = 'root', password = '', host = 'localhost')
)
db_con <- function(environ, db) {
if(environ == 'local') {
settings <- DB_CONFIG$local
} else {
settings <- DV_CONFIG$production
}
m <- DBI::dbDriver(settings$driver)
RMySQL::dbConnect('MySQL', user=settings$user, password=settings$password, host=settings$host, dbname=db);
}
## test.R . Run in command line >> " R CMD BATCH '--args local' test.R "
args <- commandArgs(TRUE)
if(length(args)==0) {
print("No arguments supplied.")
db_env <- 'local'
} else {
db_env <- args[1]
}
source('base.R')
con <- db_con(db_env, 'db_name')
res <- dbSendQuery(con, 'select * from test_table limit 3')
fetch(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment