dbDataType: Determine the SQL data type of an object

Description Usage Arguments Details Value Specification See Also Examples

Description

Returns an SQL string that describes the SQL data type to be used for an object. The default implementation of this generic determines the SQL type of an R object according to the SQL 92 specification, which may serve as a starting point for driver implementations. DBI also provides an implementation for data.frame which will return a character vector giving the type for each column in the dataframe.

\Sexpr[results=rd,stage=render]{DBI:::methods_as_rd("dbDataType")}

Usage

1
dbDataType(dbObj, obj, ...)

Arguments

dbObj

A object inheriting from DBIDriver or DBIConnection

obj

An R object whose SQL type we want to determine.

...

Other arguments passed on to methods.

Details

The data types supported by databases are different than the data types in R, but the mapping between the primitive types is straightforward:

Notice that many DBMS do not follow IEEE arithmetic, so there are potential problems with under/overflows and loss of precision.

Value

dbDataType() returns the SQL type that corresponds to the obj argument as a non-empty character string. For data frames, a character vector with one element per column is returned. An error is raised for invalid values for the obj argument such as a NULL value.

Specification

The backend can override the dbDataType() generic for its driver class.

This generic expects an arbitrary object as second argument. To query the values returned by the default implementation, run example(dbDataType, package = "DBI"). If the backend needs to override this generic, it must accept all basic R data types as its second argument, namely logical, integer, numeric, character, dates (see Dates), date-time (see DateTimeClasses), and difftime. If the database supports blobs, this method also must accept lists of raw vectors, and blob::blob objects. As-is objects (i.e., wrapped by I()) must be supported and return the same results as their unwrapped counterparts. The SQL data type for factor and ordered is the same as for character. The behavior for other object types is not specified.

All data types returned by dbDataType() are usable in an SQL statement of the form "CREATE TABLE test (a ...)".

See Also

Other DBIDriver generics: DBIDriver-class, dbCanConnect, dbConnect, dbDriver, dbGetInfo, dbIsReadOnly, dbIsValid, dbListConnections

Other DBIConnection generics: DBIConnection-class, dbAppendTable, dbCreateTable, dbDisconnect, dbExecute, dbExistsTable, dbGetException, dbGetInfo, dbGetQuery, dbIsReadOnly, dbIsValid, dbListFields, dbListObjects, dbListResults, dbListTables, dbReadTable, dbRemoveTable, dbSendQuery, dbSendStatement, dbWriteTable

Examples

 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
dbDataType(ANSI(), 1:5)
dbDataType(ANSI(), 1)
dbDataType(ANSI(), TRUE)
dbDataType(ANSI(), Sys.Date())
dbDataType(ANSI(), Sys.time())
dbDataType(ANSI(), Sys.time() - as.POSIXct(Sys.Date()))
dbDataType(ANSI(), c("x", "abc"))
dbDataType(ANSI(), list(raw(10), raw(20)))
dbDataType(ANSI(), I(3))

dbDataType(ANSI(), iris)

con <- dbConnect(RSQLite::SQLite(), ":memory:")

dbDataType(con, 1:5)
dbDataType(con, 1)
dbDataType(con, TRUE)
dbDataType(con, Sys.Date())
dbDataType(con, Sys.time())
dbDataType(con, Sys.time() - as.POSIXct(Sys.Date()))
dbDataType(con, c("x", "abc"))
dbDataType(con, list(raw(10), raw(20)))
dbDataType(con, I(3))

dbDataType(con, iris)

dbDisconnect(con)

Example output

[1] "INT"
[1] "DOUBLE"
[1] "SMALLINT"
[1] "DATE"
[1] "TIMESTAMP"
[1] "TIME"
[1] "TEXT"
[1] "BLOB"
[1] "DOUBLE"
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
    "DOUBLE"     "DOUBLE"     "DOUBLE"     "DOUBLE"       "TEXT" 
[1] "INTEGER"
[1] "REAL"
[1] "INTEGER"
[1] "REAL"
[1] "REAL"
[1] "REAL"
[1] "TEXT"
[1] "BLOB"
[1] "REAL"
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
    "DOUBLE"     "DOUBLE"     "DOUBLE"     "DOUBLE"       "TEXT" 

DBI documentation built on May 2, 2018, 5:04 p.m.