
R version 2.9.1 Patched (2009-07-09 r48925)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> ##-*- R -*-
> library(RODBC)
> library(MASS)
> USArrests[1,2] <- NA
> hills <- hills[1:15,]
> row.names(hills)[12] <- "Dollar ('$')"
> 
> 
> # DBase: maps table/column names to u/case, max length 8
> dbf <- system.file("files", "sids.dbf", package="foreign")
> unlink("sids.dbf")
> file.copy(dbf, "sids.dbf")
[1] TRUE
> channel <- odbcConnectDbase("sids.dbf")
> str(sids <- sqlFetch(channel, "sids"))
'data.frame':	100 obs. of  14 variables:
 $ AREA     : num  0.114 0.061 0.143 0.07 0.153 0.097 0.062 0.091 0.118 0.124 ...
 $ PERIMETER: num  1.44 1.23 1.63 2.97 2.21 ...
 $ CNTY_    : num  1825 1827 1828 1831 1832 ...
 $ CNTY_ID  : num  1825 1827 1828 1831 1832 ...
 $ NAME     : Factor w/ 100 levels "Alamance","Alexander",..: 5 3 86 27 66 46 15 37 93 85 ...
 $ FIPS     : int  37009 37005 37171 37053 37131 37091 37029 37073 37185 37169 ...
 $ FIPSNO   : num  37009 37005 37171 37053 37131 ...
 $ CRESS_ID : num  5 3 86 27 66 46 15 37 93 85 ...
 $ BIR74    : num  1091 487 3188 508 1421 ...
 $ SID74    : num  1 0 5 1 9 7 0 0 4 1 ...
 $ NWBIR74  : num  10 10 208 123 1066 ...
 $ BIR79    : num  1364 542 3616 830 1606 ...
 $ SID79    : num  0 3 6 2 3 5 2 2 2 5 ...
 $ NWBIR79  : num  19 12 260 145 1197 ...
> sqlUpdate(channel, sids[1:2, ], "sids", index="NAME", verbose=TRUE, fast=FALSE)
Query: UPDATE [sids] SET "AREA"=0.114, "PERIMETER"=1.442, "CNTY_"=1825, "CNTY_ID"=1825, "FIPS"='37009', "FIPSNO"=37009, "CRESS_ID"=5, "BIR74"=1091, "SID74"=1, "NWBIR74"=10, "BIR79"=1364, "SID79"=0, "NWBIR79"=19 WHERE "NAME"='Ashe'
Query: UPDATE [sids] SET "AREA"=0.061, "PERIMETER"=1.231, "CNTY_"=1827, "CNTY_ID"=1827, "FIPS"='37005', "FIPSNO"=37005, "CRESS_ID"=3, "BIR74"= 487, "SID74"=0, "NWBIR74"=10, "BIR79"= 542, "SID79"=3, "NWBIR79"=12 WHERE "NAME"='Alleghany'
> close(channel)
> 
> channel <- odbcConnectDbase("sids.dbf", case="toupper")
> sqlDrop(channel, "HILLS 2", errors=FALSE)
> sqlSave(channel, hills, "HILLS 2", verbose=TRUE)
Query: CREATE TABLE [HILLS 2]  ("rownames" Char(254), "DIST" Numeric, "CLIMB" Numeric, "TIME" Numeric)
Query: INSERT INTO [HILLS 2] ( "ROWNAMES", "DIST", "CLIMB", "TIME" ) VALUES ( ?,?,?,? )
Binding: 'ROWNAMES' DataType 12, ColSize 254
Binding: 'DIST' DataType 8, ColSize 53
Binding: 'CLIMB' DataType 8, ColSize 53
Binding: 'TIME' DataType 8, ColSize 53
Parameters:
no: 1: ROWNAMES Greenmantle/***/no: 2: DIST 2.5/***/no: 3: CLIMB 650/***/no: 4: TIME 16.083/***/
no: 1: ROWNAMES Carnethy/***/no: 2: DIST 6/***/no: 3: CLIMB 2500/***/no: 4: TIME 48.35/***/
no: 1: ROWNAMES Craig Dunain/***/no: 2: DIST 6/***/no: 3: CLIMB 900/***/no: 4: TIME 33.65/***/
no: 1: ROWNAMES Ben Rha/***/no: 2: DIST 7.5/***/no: 3: CLIMB 800/***/no: 4: TIME 45.6/***/
no: 1: ROWNAMES Ben Lomond/***/no: 2: DIST 8/***/no: 3: CLIMB 3070/***/no: 4: TIME 62.267/***/
no: 1: ROWNAMES Goatfell/***/no: 2: DIST 8/***/no: 3: CLIMB 2866/***/no: 4: TIME 73.217/***/
no: 1: ROWNAMES Bens of Jura/***/no: 2: DIST 16/***/no: 3: CLIMB 7500/***/no: 4: TIME 204.617/***/
no: 1: ROWNAMES Cairnpapple/***/no: 2: DIST 6/***/no: 3: CLIMB 800/***/no: 4: TIME 36.367/***/
no: 1: ROWNAMES Scolty/***/no: 2: DIST 5/***/no: 3: CLIMB 800/***/no: 4: TIME 29.75/***/
no: 1: ROWNAMES Traprain/***/no: 2: DIST 6/***/no: 3: CLIMB 650/***/no: 4: TIME 39.75/***/
no: 1: ROWNAMES Lairig Ghru/***/no: 2: DIST 28/***/no: 3: CLIMB 2100/***/no: 4: TIME 192.667/***/
no: 1: ROWNAMES Dollar ('$')/***/no: 2: DIST 5/***/no: 3: CLIMB 2000/***/no: 4: TIME 43.05/***/
no: 1: ROWNAMES Lomonds/***/no: 2: DIST 9.5/***/no: 3: CLIMB 2200/***/no: 4: TIME 65/***/
no: 1: ROWNAMES Cairn Table/***/no: 2: DIST 6/***/no: 3: CLIMB 500/***/no: 4: TIME 44.133/***/
no: 1: ROWNAMES Eildon Two/***/no: 2: DIST 4.5/***/no: 3: CLIMB 1500/***/no: 4: TIME 26.933/***/
> sqlUpdate(channel, hills[11:15,],  "HILLS 2", verbose=TRUE)
Query: UPDATE [HILLS 2] SET "DIST"=?, "CLIMB"=?, "TIME"=? WHERE "ROWNAMES"=?
Binding: 'DIST' DataType 8, ColSize 53
Binding: 'CLIMB' DataType 8, ColSize 53
Binding: 'TIME' DataType 8, ColSize 53
Binding: 'ROWNAMES' DataType 12, ColSize 254
Parameters:
no: 1: DIST 28/***/no: 2: CLIMB 2100/***/no: 3: TIME 192.667/***/no: 4: ROWNAMES Lairig Ghru/***/
no: 1: DIST 5/***/no: 2: CLIMB 2000/***/no: 3: TIME 43.05/***/no: 4: ROWNAMES Dollar ('$')/***/
no: 1: DIST 9.5/***/no: 2: CLIMB 2200/***/no: 3: TIME 65/***/no: 4: ROWNAMES Lomonds/***/
no: 1: DIST 6/***/no: 2: CLIMB 500/***/no: 3: TIME 44.133/***/no: 4: ROWNAMES Cairn Table/***/
no: 1: DIST 4.5/***/no: 2: CLIMB 1500/***/no: 3: TIME 26.933/***/no: 4: ROWNAMES Eildon Two/***/
> sqlDrop(channel, "HILLS 2")
> close(channel)
> 
> 
> # Excel 2003
> channel <- odbcConnectExcel("hills.xls")
> ## list the spreadsheets and marked ranges
> sqlTables(channel)
                               TABLE_CAT TABLE_SCHEM TABLE_NAME   TABLE_TYPE
1 d:\\R\\svn\\Rpkgs\\RODBC\\tests\\hills        <NA>     hills$ SYSTEM TABLE
2 d:\\R\\svn\\Rpkgs\\RODBC\\tests\\hills        <NA>     women$ SYSTEM TABLE
3 d:\\R\\svn\\Rpkgs\\RODBC\\tests\\hills        <NA>      women        TABLE
  REMARKS
1    <NA>
2    <NA>
3    <NA>
> sqlColumns(channel, "hills")
                               TABLE_CAT TABLE_SCHEM TABLE_NAME COLUMN_NAME
1 d:\\R\\svn\\Rpkgs\\RODBC\\tests\\hills        <NA>     hills$        dist
2 d:\\R\\svn\\Rpkgs\\RODBC\\tests\\hills        <NA>     hills$       climb
3 d:\\R\\svn\\Rpkgs\\RODBC\\tests\\hills        <NA>     hills$       ttime
  DATA_TYPE TYPE_NAME COLUMN_SIZE BUFFER_LENGTH DECIMAL_DIGITS NUM_PREC_RADIX
1         8    NUMBER          53             8             NA              2
2         8    NUMBER          53             8             NA              2
3         8    NUMBER          53             8             NA              2
  NULLABLE REMARKS COLUMN_DEF SQL_DATA_TYPE SQL_DATETIME_SUB CHAR_OCTET_LENGTH
1        1    <NA>       <NA>             8               NA                NA
2        1    <NA>       <NA>             8               NA                NA
3        1    <NA>       <NA>             8               NA                NA
  ORDINAL_POSITION IS_NULLABLE ORDINAL
1                1         YES       1
2                2         YES       2
3                3         YES       3
> ## two ways to retrieve the contents of hills
> sqlFetch(channel, "hills")
   dist climb   ttime
1   2.5   650  16.083
2   6.0  2500  48.350
3   6.0   900  33.650
4   7.5   800  45.600
5   8.0  3070  62.267
6   8.0  2866  73.217
7  16.0  7500 204.617
8   6.0   800  36.367
9   5.0   800  29.750
10  6.0   650  39.750
11 28.0  2100 192.667
12  5.0  2000  43.050
13  9.5  2200  65.000
14  6.0   500  44.133
15  4.5  1500  26.933
16 10.0  3000  72.250
17 14.0  2200  98.417
18  3.0   350  78.650
19  4.5  1000  17.417
20  5.5   600  32.567
21  3.0   300  15.950
22  3.5  1500  27.900
23  6.0  2200  47.633
24  2.0   900  17.933
25  3.0   600  18.683
26  4.0  2000  26.217
27  6.0   800  34.433
28  5.0   950  28.567
29  6.5  1750  50.500
30  5.0   500  20.950
31 10.0  4400  85.583
32  6.0   600  32.383
33 18.0  5200 170.250
34  4.5   850  28.100
35 20.0  5000 159.833
> sqlQuery(channel, "select * from [hills$]")
   dist climb   ttime
1   2.5   650  16.083
2   6.0  2500  48.350
3   6.0   900  33.650
4   7.5   800  45.600
5   8.0  3070  62.267
6   8.0  2866  73.217
7  16.0  7500 204.617
8   6.0   800  36.367
9   5.0   800  29.750
10  6.0   650  39.750
11 28.0  2100 192.667
12  5.0  2000  43.050
13  9.5  2200  65.000
14  6.0   500  44.133
15  4.5  1500  26.933
16 10.0  3000  72.250
17 14.0  2200  98.417
18  3.0   350  78.650
19  4.5  1000  17.417
20  5.5   600  32.567
21  3.0   300  15.950
22  3.5  1500  27.900
23  6.0  2200  47.633
24  2.0   900  17.933
25  3.0   600  18.683
26  4.0  2000  26.217
27  6.0   800  34.433
28  5.0   950  28.567
29  6.5  1750  50.500
30  5.0   500  20.950
31 10.0  4400  85.583
32  6.0   600  32.383
33 18.0  5200 170.250
34  4.5   850  28.100
35 20.0  5000 159.833
> 
> sqlFetch(channel, "women")
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164
> close(channel)
> 
> # The Excel driver maps ' ' to '_'.
> channel <- odbcConnectExcel("hills.xls", readOnly=FALSE)
> sqlSave(channel, hills, "hills_test", verbose=TRUE, fast=FALSE)
Query: CREATE TABLE [hills_test]  ("rownames" VARCHAR(255), "dist" NUMBER, "climb" NUMBER, "time" NUMBER)
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Greenmantle',  2.5,  650,  16.083 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Carnethy',  6.0, 2500,  48.350 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Craig Dunain',  6.0,  900,  33.650 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Ben Rha',  7.5,  800,  45.600 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Ben Lomond',  8.0, 3070,  62.267 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Goatfell',  8.0, 2866,  73.217 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Bens of Jura', 16.0, 7500, 204.617 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Cairnpapple',  6.0,  800,  36.367 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Scolty',  5.0,  800,  29.750 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Traprain',  6.0,  650,  39.750 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Lairig Ghru', 28.0, 2100, 192.667 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Dollar (''$'')',  5.0, 2000,  43.050 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Lomonds',  9.5, 2200,  65.000 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Cairn Table',  6.0,  500,  44.133 )
Query: INSERT INTO [hills_test] ( "rownames", "dist", "climb", "time" ) VALUES ( 'Eildon Two',  4.5, 1500,  26.933 )
> sqlUpdate(channel, hills[11:15,], "hills_test", verbose=TRUE, fast=FALSE)
Query: UPDATE [hills_test] SET "dist"=28.0, "climb"=2100, "time"=192.667 WHERE "rownames"='Lairig Ghru'
Query: UPDATE [hills_test] SET "dist"= 5.0, "climb"=2000, "time"= 43.050 WHERE "rownames"='Dollar (''$'')'
Query: UPDATE [hills_test] SET "dist"= 9.5, "climb"=2200, "time"= 65.000 WHERE "rownames"='Lomonds'
Query: UPDATE [hills_test] SET "dist"= 6.0, "climb"= 500, "time"= 44.133 WHERE "rownames"='Cairn Table'
Query: UPDATE [hills_test] SET "dist"= 4.5, "climb"=1500, "time"= 26.933 WHERE "rownames"='Eildon Two'
> sqlFetch(channel, "hills_test")
             dist climb    time
Greenmantle   2.5   650  16.083
Carnethy      6.0  2500  48.350
Craig Dunain  6.0   900  33.650
Ben Rha       7.5   800  45.600
Ben Lomond    8.0  3070  62.267
Goatfell      8.0  2866  73.217
Bens of Jura 16.0  7500 204.617
Cairnpapple   6.0   800  36.367
Scolty        5.0   800  29.750
Traprain      6.0   650  39.750
Lairig Ghru  28.0  2100 192.667
Dollar ('$')  5.0  2000  43.050
Lomonds       9.5  2200  65.000
Cairn Table   6.0   500  44.133
Eildon Two    4.5  1500  26.933
> sqlSave(channel, hills, "hills_test2", verbose=TRUE)
Query: CREATE TABLE [hills_test2]  ("rownames" VARCHAR(255), "dist" NUMBER, "climb" NUMBER, "time" NUMBER)
Query: INSERT INTO [hills_test2] ( "rownames", "dist", "climb", "time" ) VALUES ( ?,?,?,? )
Binding: 'rownames' DataType 12, ColSize 255
Binding: 'dist' DataType 8, ColSize 53
Binding: 'climb' DataType 8, ColSize 53
Binding: 'time' DataType 8, ColSize 53
Parameters:
no: 1: rownames Greenmantle/***/no: 2: dist 2.5/***/no: 3: climb 650/***/no: 4: time 16.083/***/
no: 1: rownames Carnethy/***/no: 2: dist 6/***/no: 3: climb 2500/***/no: 4: time 48.35/***/
no: 1: rownames Craig Dunain/***/no: 2: dist 6/***/no: 3: climb 900/***/no: 4: time 33.65/***/
no: 1: rownames Ben Rha/***/no: 2: dist 7.5/***/no: 3: climb 800/***/no: 4: time 45.6/***/
no: 1: rownames Ben Lomond/***/no: 2: dist 8/***/no: 3: climb 3070/***/no: 4: time 62.267/***/
no: 1: rownames Goatfell/***/no: 2: dist 8/***/no: 3: climb 2866/***/no: 4: time 73.217/***/
no: 1: rownames Bens of Jura/***/no: 2: dist 16/***/no: 3: climb 7500/***/no: 4: time 204.617/***/
no: 1: rownames Cairnpapple/***/no: 2: dist 6/***/no: 3: climb 800/***/no: 4: time 36.367/***/
no: 1: rownames Scolty/***/no: 2: dist 5/***/no: 3: climb 800/***/no: 4: time 29.75/***/
no: 1: rownames Traprain/***/no: 2: dist 6/***/no: 3: climb 650/***/no: 4: time 39.75/***/
no: 1: rownames Lairig Ghru/***/no: 2: dist 28/***/no: 3: climb 2100/***/no: 4: time 192.667/***/
no: 1: rownames Dollar ('$')/***/no: 2: dist 5/***/no: 3: climb 2000/***/no: 4: time 43.05/***/
no: 1: rownames Lomonds/***/no: 2: dist 9.5/***/no: 3: climb 2200/***/no: 4: time 65/***/
no: 1: rownames Cairn Table/***/no: 2: dist 6/***/no: 3: climb 500/***/no: 4: time 44.133/***/
no: 1: rownames Eildon Two/***/no: 2: dist 4.5/***/no: 3: climb 1500/***/no: 4: time 26.933/***/
> sqlUpdate(channel, hills[11:15,], "hills_test2", verbose=TRUE)
Query: UPDATE [hills_test2] SET "dist"=?, "climb"=?, "time"=? WHERE "rownames"=?
Binding: 'dist' DataType 8, ColSize 53
Binding: 'climb' DataType 8, ColSize 53
Binding: 'time' DataType 8, ColSize 53
Binding: 'rownames' DataType 12, ColSize 255
Parameters:
no: 1: dist 28/***/no: 2: climb 2100/***/no: 3: time 192.667/***/no: 4: rownames Lairig Ghru/***/
no: 1: dist 5/***/no: 2: climb 2000/***/no: 3: time 43.05/***/no: 4: rownames Dollar ('$')/***/
no: 1: dist 9.5/***/no: 2: climb 2200/***/no: 3: time 65/***/no: 4: rownames Lomonds/***/
no: 1: dist 6/***/no: 2: climb 500/***/no: 3: time 44.133/***/no: 4: rownames Cairn Table/***/
no: 1: dist 4.5/***/no: 2: climb 1500/***/no: 3: time 26.933/***/no: 4: rownames Eildon Two/***/
> sqlFetch(channel, "hills_test2")
             dist climb    time
Greenmantle   2.5   650  16.083
Carnethy      6.0  2500  48.350
Craig Dunain  6.0   900  33.650
Ben Rha       7.5   800  45.600
Ben Lomond    8.0  3070  62.267
Goatfell      8.0  2866  73.217
Bens of Jura 16.0  7500 204.617
Cairnpapple   6.0   800  36.367
Scolty        5.0   800  29.750
Traprain      6.0   650  39.750
Lairig Ghru  28.0  2100 192.667
Dollar ('$')  5.0  2000  43.050
Lomonds       9.5  2200  65.000
Cairn Table   6.0   500  44.133
Eildon Two    4.5  1500  26.933
> close(channel)
