-
Notifications
You must be signed in to change notification settings - Fork 84
/
Connect to DB
43 lines (32 loc) · 1.06 KB
/
Connect to DB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#RODBC documentation: http://cran.r-project.org/web/packages/RODBC/RODBC.pdf
#http://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf
#Check data base connection using run>odbcad32
library(RODBC)
downloadDir="C:/Users/erik.johansson/Downloads"
setwd(downloadDir)
forever="yes"
while(forever=="yes") {
gt_connector=odbcConnect("ejjohans_googletrends", uid="ejjohans_gt", pwd="***")
#Check that table is available in R
sqlTables(gt_connector, tableType="TABLE")
#Get keyword from data base
start_keyword=as.character(sqlFetch(gt_connector, "keyword_table")[1,1])
keyword=start_keyword
while(start_keyword==keyword) {
keyword=as.character(sqlFetch(gt_connector, "keyword_table")[1,1])
Sys.sleep(5)
}
#Create URL
URL=URL_GT(keyword)
filePath=downloadGT(URL, downloadDir)
data=readGT(filePath)
data=data[,c(3,1,2)]
output=data
sqlDrop(gt_connector, "output", errors=FALSE)
sqlDrop(gt_connector, "geodata", errors=FALSE)
sqlSave(gt_connector, output)
geodata=readGeoGT(filePath)
names(geodata)=c("Region", "SVI", "Source")
sqlSave(gt_connector, geodata)
}
close(gt_connector)