
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 ('$')"
> 
> channel <- odbcConnect("testacc")
> if(!inherits(channel, "RODBC")) q("no")
> sqlTables(channel)
         TABLE_CAT TABLE_SCHEM        TABLE_NAME   TABLE_TYPE REMARKS
1 D:\\bdr\\testacc        <NA>          MSysACEs SYSTEM TABLE    <NA>
2 D:\\bdr\\testacc        <NA>       MSysObjects SYSTEM TABLE    <NA>
3 D:\\bdr\\testacc        <NA>       MSysQueries SYSTEM TABLE    <NA>
4 D:\\bdr\\testacc        <NA> MSysRelationships SYSTEM TABLE    <NA>
> sqlDrop(channel, "USArrests", errors = FALSE)
> sqlSave(channel, USArrests)
> sqlTables(channel)
         TABLE_CAT TABLE_SCHEM        TABLE_NAME   TABLE_TYPE REMARKS
1 D:\\bdr\\testacc        <NA>          MSysACEs SYSTEM TABLE    <NA>
2 D:\\bdr\\testacc        <NA>       MSysObjects SYSTEM TABLE    <NA>
3 D:\\bdr\\testacc        <NA>       MSysQueries SYSTEM TABLE    <NA>
4 D:\\bdr\\testacc        <NA> MSysRelationships SYSTEM TABLE    <NA>
5 D:\\bdr\\testacc        <NA>         USArrests        TABLE    <NA>
> sqlColumns(channel, "USArrests")
         TABLE_CAT TABLE_SCHEM TABLE_NAME COLUMN_NAME DATA_TYPE TYPE_NAME
1 D:\\bdr\\testacc        <NA>  USArrests    rownames        12   VARCHAR
2 D:\\bdr\\testacc        <NA>  USArrests      Murder         8    DOUBLE
3 D:\\bdr\\testacc        <NA>  USArrests     Assault         4   INTEGER
4 D:\\bdr\\testacc        <NA>  USArrests    UrbanPop         4   INTEGER
5 D:\\bdr\\testacc        <NA>  USArrests        Rape         8    DOUBLE
  COLUMN_SIZE BUFFER_LENGTH DECIMAL_DIGITS NUM_PREC_RADIX NULLABLE REMARKS
1         255           510             NA             NA        1    <NA>
2          53             8             NA              2        1    <NA>
3          10             4              0             10        1    <NA>
4          10             4              0             10        1    <NA>
5          53             8             NA              2        1    <NA>
  COLUMN_DEF SQL_DATA_TYPE SQL_DATETIME_SUB CHAR_OCTET_LENGTH ORDINAL_POSITION
1       <NA>            12               NA               510                1
2       <NA>             8               NA                NA                2
3       <NA>             4               NA                NA                3
4       <NA>             4               NA                NA                4
5       <NA>             8               NA                NA                5
  IS_NULLABLE ORDINAL
1         YES       1
2         YES       2
3         YES       3
4         YES       4
5         YES       5
> sqlPrimaryKeys(channel, "USArrests") # reports none
> sqlFetch(channel, "USArrests")
               Murder Assault UrbanPop Rape
Alabama          13.2      NA       58 21.2
Alaska           10.0     263       48 44.5
Arizona           8.1     294       80 31.0
Arkansas          8.8     190       50 19.5
California        9.0     276       91 40.6
Colorado          7.9     204       78 38.7
Connecticut       3.3     110       77 11.1
Delaware          5.9     238       72 15.8
Florida          15.4     335       80 31.9
Georgia          17.4     211       60 25.8
Hawaii            5.3      46       83 20.2
Idaho             2.6     120       54 14.2
Illinois         10.4     249       83 24.0
Indiana           7.2     113       65 21.0
Iowa              2.2      56       57 11.3
Kansas            6.0     115       66 18.0
Kentucky          9.7     109       52 16.3
Louisiana        15.4     249       66 22.2
Maine             2.1      83       51  7.8
Maryland         11.3     300       67 27.8
Massachusetts     4.4     149       85 16.3
Michigan         12.1     255       74 35.1
Minnesota         2.7      72       66 14.9
Mississippi      16.1     259       44 17.1
Missouri          9.0     178       70 28.2
Montana           6.0     109       53 16.4
Nebraska          4.3     102       62 16.5
Nevada           12.2     252       81 46.0
New Hampshire     2.1      57       56  9.5
New Jersey        7.4     159       89 18.8
New Mexico       11.4     285       70 32.1
New York         11.1     254       86 26.1
North Carolina   13.0     337       45 16.1
North Dakota      0.8      45       44  7.3
Ohio              7.3     120       75 21.4
Oklahoma          6.6     151       68 20.0
Oregon            4.9     159       67 29.3
Pennsylvania      6.3     106       72 14.9
Rhode Island      3.4     174       87  8.3
South Carolina   14.4     279       48 22.5
South Dakota      3.8      86       45 12.8
Tennessee        13.2     188       59 26.9
Texas            12.7     201       80 25.5
Utah              3.2     120       80 22.9
Vermont           2.2      48       32 11.2
Virginia          8.5     156       63 20.7
Washington        4.0     145       73 26.2
West Virginia     5.7      81       39  9.3
Wisconsin         2.6      53       66 10.8
Wyoming           6.8     161       60 15.6
> query <- paste("select rownames, murder from USArrests",
+                "where Rape > 30",  "order by Murder")
> sqlQuery(channel, query)
    rownames murder
1   Colorado    7.9
2    Arizona    8.1
3 California    9.0
4     Alaska   10.0
5 New Mexico   11.4
6   Michigan   12.1
7     Nevada   12.2
8    Florida   15.4
> sqlCopy(channel, query, "HighRape", rownames = FALSE)
> sqlFetch(channel, "HighRape", max = 5)
           murder
Colorado      7.9
Arizona       8.1
California    9.0
Alaska       10.0
New Mexico   11.4
> sqlTables(channel)
         TABLE_CAT TABLE_SCHEM        TABLE_NAME   TABLE_TYPE REMARKS
1 D:\\bdr\\testacc        <NA>          MSysACEs SYSTEM TABLE    <NA>
2 D:\\bdr\\testacc        <NA>       MSysObjects SYSTEM TABLE    <NA>
3 D:\\bdr\\testacc        <NA>       MSysQueries SYSTEM TABLE    <NA>
4 D:\\bdr\\testacc        <NA> MSysRelationships SYSTEM TABLE    <NA>
5 D:\\bdr\\testacc        <NA>          HighRape        TABLE    <NA>
6 D:\\bdr\\testacc        <NA>         USArrests        TABLE    <NA>
> sqlDrop(channel, "HighRape")
> foo <-  USArrests[1:3, 2, drop = FALSE]
> foo[1,1] <- 236
> sqlUpdate(channel, foo, "USArrests")
> sqlFetch(channel, "USArrests", max = 5)
           Murder Assault UrbanPop Rape
Alabama      13.2     236       58 21.2
Alaska       10.0     263       48 44.5
Arizona       8.1     294       80 31.0
Arkansas      8.8     190       50 19.5
California    9.0     276       91 40.6
> sqlFetchMore(channel, max = 8)
            Murder Assault UrbanPop Rape
Colorado       7.9     204       78 38.7
Connecticut    3.3     110       77 11.1
Delaware       5.9     238       72 15.8
Florida       15.4     335       80 31.9
Georgia       17.4     211       60 25.8
Hawaii         5.3      46       83 20.2
Idaho          2.6     120       54 14.2
Illinois      10.4     249       83 24.0
> sqlDrop(channel, "USArrests")
> 
> dates <- as.character(seq(as.Date("2004-01-01"), by="week", length=10))
> Dtest <- data.frame(dates)
> sqlDrop(channel, "Dtest", errors = FALSE)
> varspec <- "DATETIME"; names(varspec) <- names(Dtest)
> ## fast = TRUE crashes
> sqlSave(channel, Dtest, varTypes = varspec, verbose=TRUE, fast=FALSE)
Query: CREATE TABLE "Dtest"  ("rownames" VARCHAR(255), "dates" DATETIME)
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '1', #2004-01-01# )
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '2', #2004-01-08# )
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '3', #2004-01-15# )
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '4', #2004-01-22# )
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '5', #2004-01-29# )
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '6', #2004-02-05# )
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '7', #2004-02-12# )
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '8', #2004-02-19# )
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '9', #2004-02-26# )
Query: INSERT INTO "Dtest" ( "rownames", "dates" ) VALUES ( '10', #2004-03-04# )
> sqlColumns(channel, "Dtest")
         TABLE_CAT TABLE_SCHEM TABLE_NAME COLUMN_NAME DATA_TYPE TYPE_NAME
1 D:\\bdr\\testacc        <NA>      Dtest    rownames        12   VARCHAR
2 D:\\bdr\\testacc        <NA>      Dtest       dates        93  DATETIME
  COLUMN_SIZE BUFFER_LENGTH DECIMAL_DIGITS NUM_PREC_RADIX NULLABLE REMARKS
1         255           510             NA             NA        1    <NA>
2          19            16              0             NA        1    <NA>
  COLUMN_DEF SQL_DATA_TYPE SQL_DATETIME_SUB CHAR_OCTET_LENGTH ORDINAL_POSITION
1       <NA>            12               NA               510                1
2       <NA>             9                3                NA                2
  IS_NULLABLE ORDINAL
1         YES       1
2         YES       2
> sqlFetch(channel, "Dtest")
        dates
1  2004-01-01
2  2004-01-08
3  2004-01-15
4  2004-01-22
5  2004-01-29
6  2004-02-05
7  2004-02-12
8  2004-02-19
9  2004-02-26
10 2004-03-04
> sqlDrop(channel, "Dtest")
> 
> sqlDrop(channel, "hills test", errors = FALSE)
> sqlSave(channel, hills, "hills test", verbose=TRUE)
Query: CREATE TABLE "hills test"  ("rownames" VARCHAR(255), "dist" DOUBLE, "climb" INTEGER, "time" DOUBLE)
Query: INSERT INTO "hills test" ( "rownames", "dist", "climb", "time" ) VALUES ( ?,?,?,? )
Binding: 'rownames' DataType 12, ColSize 255
Binding: 'dist' DataType 8, ColSize 53
Binding: 'climb' DataType 4, ColSize 10
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 test", verbose=TRUE, fast=TRUE)
Query: UPDATE "hills test" SET "dist"=?, "climb"=?, "time"=? WHERE "rownames"=?
Binding: 'dist' DataType 8, ColSize 53
Binding: 'climb' DataType 4, ColSize 10
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 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
> sqlDrop(channel, "hills test")
> sqlSave(channel, hills, "hills test", verbose=TRUE, fast=FALSE)
Query: CREATE TABLE "hills test"  ("rownames" VARCHAR(255), "dist" DOUBLE, "climb" INTEGER, "time" DOUBLE)
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'
> sqlDrop(channel, "hills test")
> 
> close(channel)
