odbcDataType: Return the corresponding ODBC data type for an R object

Description Usage Arguments Details Value Defining a new dbDataType method

View source: R/DataTypes.R

Description

This is used when creating a new table with dbWriteTable(). Databases with default methods defined are

Usage

1
odbcDataType(con, obj, ...)

Arguments

con

A driver connection object, as returned by dbConnect().

obj

An R object.

...

Additional arguments passed to methods.

Details

If you are using a different database and dbWriteTable() fails with a SQL parsing error the default method is not appropriate, you will need to write a new method.

Value

Corresponding SQL type for the obj.

Defining a new dbDataType method

The object type for your connection will be the database name retrieved by dbGetInfo(con)$dbms.name. Use the documentation provided with your database to determine appropriate values for each R data type. An example method definition of a fictional foo database follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
con <- dbConnect(odbc::odbc(), "FooConnection")
dbGetInfo(con)$dbms.name
#> [1] "foo"

`odbcDataType.foo <- function(con, obj, ...) {
  switch_type(obj,
    factor = "VARCHAR(255)",
    datetime = "TIMESTAMP",
    date = "DATE",
    binary = "BINARY",
    integer = "INTEGER",
    double = "DOUBLE",
    character = "VARCHAR(255)",
    logical = "BIT",
    list = "VARCHAR(255)",
    stop("Unsupported type", call. = FALSE)
  )
}

odbc documentation built on Dec. 11, 2021, 9:31 a.m.