Created
October 28, 2013 07:49
-
-
Save kilfu0701/7192862 to your computer and use it in GitHub Desktop.
R mysql connect testing...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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