Skip to content

Instantly share code, notes, and snippets.

@qoelet
Created February 20, 2014 09:06
Show Gist options
  • Save qoelet/9109654 to your computer and use it in GitHub Desktop.
Save qoelet/9109654 to your computer and use it in GitHub Desktop.
library(RMySQL)
library(GetoptLong)
# had to work with a secure connection to a MySQL
# make a option file, e.g. opts.cnf
"
[client]
user=kenny
host=mysqldb
ssl-ca=ca.crt
ssl-cert=cert.crt
ssl-key=mykey.key
"
# other assignments
# e.g. df_name = "extraction_{{country}}"
# run query multiple times across different country tables
countries = c("singapore", "malaysia", "thailand")
for (country in countries) {
country_query = qq(query, code.pattern="\\{\\{CODE\\}\\}")
country_df_name = qq(df_name, code.pattern="\\{\\{CODE\\}\\}")
con = dbConnect(MySQL(), user="kenny", dbname=db, host="mysqldb", default.file="opts.cnf")
tmp_df = dbGetQuery(con, country_query)
assign(country_df_name, tmp_df)
dbDisconnect(con)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment