Skip to content

Instantly share code, notes, and snippets.

@qoelet
Created February 20, 2014 09:06

Revisions

  1. ____________. created this gist Feb 20, 2014.
    27 changes: 27 additions & 0 deletions mysql_in_R.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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)
    }