diff --git a/R/redshift.r b/R/redshift.r index b29c56b..74b47a1 100644 --- a/R/redshift.r +++ b/R/redshift.r @@ -55,3 +55,14 @@ redshift.unload <- function(conn, query, filename, aws.accesskey, aws.secretkey, print(unload.query) dbSendUpdate(conn,unload.query) } + +redshift.insertTable = function(conn, dataframe, rs.tablename){ + insertValuesString = paste(apply(dataframe,1,function(x){ + paste0("(",paste(sapply(x,function(val){ + paste0("'",gsub("'","''",val),"'") + }),collapse = ","),")") + }),collapse = ',') + + insertQuery = paste0("Insert into ",rs.tablename," values ",insertValuesString) + dbSendUpdate(conn,insertQuery) +} \ No newline at end of file diff --git a/man/redshift.insertTable.Rd b/man/redshift.insertTable.Rd new file mode 100644 index 0000000..8a7d2b1 --- /dev/null +++ b/man/redshift.insertTable.Rd @@ -0,0 +1,27 @@ +\name{redshift.insertTable} +\alias{redshift.insertTable} +\title{Insert a data.frame into a redshift table} +\description{ +Insert a data.frame into a redshift table. Encoding must be utf-8. Use iconv() to enforce so. +} +\usage{ +redshift.insertTable(conn, dataframe, rs.tablename) +} +\arguments{ + \item{conn}{The database connection} + \item{dataframe}{The R dataframe to be inserted} + \item{rs.tablename}{The destination Redshift table} +} +\value{ +NA +} +\author{ +Eeshan Chatterjee +} +\examples{ +require(redshift) +# conn <- redshift.connect("jdbc:postgresql://blah.blah.eu-west-1.redshift.amazonaws.com:5439/data", "username", "password") +#redshift.insertTable(conn,dataframe = myDF, rs.tablename = "myRSTable") +} +\keyword{ insert table } +