Guest User

Untitled

a guest
Feb 7th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. # DB Handle for config file #
  2. dbhandle <- odbcDriverConnect()
  3.  
  4. # save the data in the table finally
  5. sqlSave(dbhandle, bp, "FACT_OP", append=TRUE, rownames=FALSE, verbose = verbose, fast = TRUE)
  6. # varTypes <- c(Date="datetime", QueryDate = "datetime")
  7. # sqlSave(dbhandle, bp, "FACT_OP", rownames=FALSE,verbose = TRUE, fast = TRUE, varTypes=varTypes)
  8.  
  9. # DB handle close
  10. odbcClose(dbhandle)
  11.  
  12. toSQL = data.frame(...);
  13. write.table(toSQL,"C:\export\filename.txt",quote=FALSE,sep=",",row.names=FALSE,col.names=FALSE,append=FALSE);
  14. sqlQuery(channel,"BULK
  15. INSERT Yada.dbo.yada
  16. FROM '\\<server-that-SQL-server-can-see>\export\filename.txt'
  17. WITH
  18. (
  19. FIELDTERMINATOR = ',',
  20. ROWTERMINATOR = '\n'
  21. )");
  22.  
  23. library(RODBC)
  24. channel <- ...
  25. dataTable <- ...relevant data...
  26. numberOfThousands <- floor(nrow(dataTable)/1000)
  27. extra <- nrow(dataTable)%%1000
  28.  
  29. thousandInsertQuery <- function(channel,dat,range){
  30. sqlQuery(channel,paste0("INSERT INTO Database.dbo.Responses (IDNum,State,Answer)
  31. VALUES "
  32. ,paste0(
  33. sapply(range,function(k) {
  34. paste0("(",dat$IDNum[k],",'",
  35. dat$State[k],"','",
  36. gsub("'","''",dat$Answer[k],fixed=TRUE),"')")
  37. })
  38. ,collapse=",")))
  39. }
  40.  
  41. if(numberOfThousands)
  42. for(n in 1:numberOfThousands)
  43. {
  44. thousandInsertQuery(channel,(1000*(n-1)+1):(1000*n),dataTable)
  45. }
  46. if(extra)
  47. thousandInsertQuery(channel,(1000*numberOfThousands+1):(1000*numberOfThousands+extra))
  48.  
  49. library(DBI)
  50. library(RJDBC)
  51. library(tidyverse)
  52.  
  53. # Specify where you driver lives
  54. drv <- JDBC(
  55. "com.microsoft.sqlserver.jdbc.SQLServerDriver",
  56. "c:/R/SQL/sqljdbc42.jar")
  57.  
  58. # Connect to AWS RDS instance
  59. conn <- drv %>%
  60. dbConnect(
  61. host = "jdbc:sqlserver://xxx.ccgqenhjdi18.ap-southeast-2.rds.amazonaws.com",
  62. user = "xxx",
  63. password = "********",
  64. port = 1433,
  65. dbname= "qlik")
  66.  
  67. if(0) { # check what the conn object has access to
  68. queryResults <- conn %>%
  69. dbGetQuery("select * from information_schema.tables")
  70. }
  71.  
  72. # Create test data
  73. example_data <- data.frame(animal=c("dog", "cat", "sea cucumber", "sea urchin"),
  74. feel=c("furry", "furry", "squishy", "spiny"),
  75. weight=c(45, 8, 1.1, 0.8))
  76. # Works in 20ms in my case
  77. system.time(
  78. conn %>% dbWriteTable(
  79. "qlik.export.test",
  80. example_data
  81. )
  82. )
  83.  
  84. # Let us see if we see the exported results
  85. conn %>% dbGetQuery("select * FROM qlik.export.test")
  86.  
  87. # Let's clean the mess and force-close connection at the end of the process
  88. conn %>% dbDisconnect()
Add Comment
Please, Sign In to add comment