Title: | Provides Access to Databases Through the ODBC Interface |
---|---|
Description: | An implementation of R's DBI interface using ODBC package as a back-end. This allows R to connect to any DBMS that has a ODBC driver. |
Authors: | Nagi Teramo [aut, cre], Shinichi Takayanagi [aut] |
Maintainer: | Nagi Teramo <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2024-10-27 03:48:44 UTC |
Source: | https://github.com/teramonagi/rodbcdbi |
These methods are straight-forward implementations of the corresponding generic functions.
## S4 method for signature 'ODBCDriver' dbConnect(drv, dsn, user = NULL, password = NULL, connection, ...)
## S4 method for signature 'ODBCDriver' dbConnect(drv, dsn, user = NULL, password = NULL, connection, ...)
drv |
an object of class ODBCDriver |
dsn |
Data source name you defined by ODBC data source administrator tool. |
user |
User name to connect as. |
password |
Password to be used if the DSN demands password authentication. |
connection |
Connection string to use to connect to the ODBC database server (as per |
... |
Other parameters passed on to methods |
## Not run: # Connect to a ODBC data source con <- dbConnect(RODBCDBI::ODBC(), dsn="test") # or using a connection string con <- dbConnect(RODBCDBI::ODBC(), connection="driver={SQL Server};server=mysqlhost; database=mydbname;trusted_connection=true") # Always cleanup by disconnecting the database #' dbDisconnect(con) ## End(Not run)
## Not run: # Connect to a ODBC data source con <- dbConnect(RODBCDBI::ODBC(), dsn="test") # or using a connection string con <- dbConnect(RODBCDBI::ODBC(), connection="driver={SQL Server};server=mysqlhost; database=mydbname;trusted_connection=true") # Always cleanup by disconnecting the database #' dbDisconnect(con) ## End(Not run)
Close a current session.
## S4 method for signature 'ODBCConnection' dbDisconnect(conn)
## S4 method for signature 'ODBCConnection' dbDisconnect(conn)
conn |
a |
## Not run: library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbDisconnect(con) ## End(Not run)
## Not run: library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbDisconnect(con) ## End(Not run)
Does the table exist?
## S4 method for signature 'ODBCConnection,character' dbExistsTable(conn, name)
## S4 method for signature 'ODBCConnection,character' dbExistsTable(conn, name)
conn |
An existing |
name |
String, name of table. Match is case insensitive. |
boolean value which indicated whether the table exists or not
Get DBMS metadata.
## S4 method for signature 'ODBCConnection' dbGetInfo(dbObj, ...)
## S4 method for signature 'ODBCConnection' dbGetInfo(dbObj, ...)
dbObj |
An object inheriting from |
... |
Other parameters passed on to methods |
List fields in specified table.
## S4 method for signature 'ODBCConnection,character' dbListFields(conn, name)
## S4 method for signature 'ODBCConnection,character' dbListFields(conn, name)
conn |
An existing |
name |
a length 1 character vector giving the name of a table. |
## Not run: library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "iris", iris, overwrite=TRUE) dbListFields(con, "iris") dbDisconnect(con) ## End(Not run)
## Not run: library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "iris", iris, overwrite=TRUE) dbListFields(con, "iris") dbDisconnect(con) ## End(Not run)
List available ODBC tables.
## S4 method for signature 'ODBCConnection' dbListTables(conn)
## S4 method for signature 'ODBCConnection' dbListTables(conn)
conn |
An existing |
These functions mimic their R/S-Plus counterpart get
, assign
,
exists
, remove
, and objects
, except that they generate
code that gets remotely executed in a database engine.
## S4 method for signature 'ODBCConnection,character' dbReadTable(conn, name, row.names = NA, check.names = TRUE, select.cols = "*")
## S4 method for signature 'ODBCConnection,character' dbReadTable(conn, name, row.names = NA, check.names = TRUE, select.cols = "*")
conn |
a |
name |
a character string specifying a table name. |
row.names |
a character string specifying a table name. |
check.names |
If |
select.cols |
A SQL statement (in the form of a character vector of length 1) giving the columns to select. E.g. "*" selects all columns, "x,y,z" selects three columns named as listed. |
A data.frame in the case of dbReadTable
; otherwise a logical
indicating whether the operation was successful.
Note that the data.frame returned by dbReadTable
only has
primitive data, e.g., it does not coerce character data to factors.
## Not run: library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE) dbReadTable(con, "mtcars") dbGetQuery(con, "SELECT * FROM mtcars WHERE cyl = 8") # Supress row names dbReadTable(con, "mtcars", row.names = FALSE) dbGetQuery(con, "SELECT * FROM mtcars WHERE cyl = 8", row.names = FALSE) dbDisconnect(con) ## End(Not run)
## Not run: library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE) dbReadTable(con, "mtcars") dbGetQuery(con, "SELECT * FROM mtcars WHERE cyl = 8") # Supress row names dbReadTable(con, "mtcars", row.names = FALSE) dbGetQuery(con, "SELECT * FROM mtcars WHERE cyl = 8", row.names = FALSE) dbDisconnect(con) ## End(Not run)
Executes the SQL DROP TABLE
.
## S4 method for signature 'ODBCConnection,character' dbRemoveTable(conn, name)
## S4 method for signature 'ODBCConnection,character' dbRemoveTable(conn, name)
conn |
An existing |
name |
character vector of length 1 giving name of table to remove |
To retrieve results a chunk at a time, use dbSendQuery
,
dbFetch
, then ClearResult
. Alternatively, if you want all the
results (and they'll fit in memory) use dbGetQuery
which sends,
fetches and clears for you.
## S4 method for signature 'ODBCConnection' dbSendQuery(conn, statement, ...) ## S4 method for signature 'ODBCResult' dbFetch(res, n = -1, ...) ## S4 method for signature 'ODBCResult' dbHasCompleted(res, ...) ## S4 method for signature 'ODBCResult' dbClearResult(res, ...)
## S4 method for signature 'ODBCConnection' dbSendQuery(conn, statement, ...) ## S4 method for signature 'ODBCResult' dbFetch(res, n = -1, ...) ## S4 method for signature 'ODBCResult' dbHasCompleted(res, ...) ## S4 method for signature 'ODBCResult' dbClearResult(res, ...)
conn |
An existing |
statement |
The SQL which you want to run |
... |
Other parameters passed on to methods |
res |
An object of class |
n |
Number of rows to return. If less than zero returns all rows. |
Write a local data frame or file to the database.
## S4 method for signature 'ODBCConnection,character,data.frame' dbWriteTable(conn, name, value, overwrite = FALSE, append = FALSE, ...)
## S4 method for signature 'ODBCConnection,character,data.frame' dbWriteTable(conn, name, value, overwrite = FALSE, append = FALSE, ...)
conn |
a |
name |
a character string specifying a table name. ODBCConnection table names
are not case sensitive, e.g., table names |
value |
a data.frame (or coercible to data.frame) object or a
file name (character). when |
overwrite |
logical. Should data be overwritten? |
append |
logical. Should data be appended to an existing table? |
... |
additional arguments passed to the generic. |
## Not run: library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE) dbReadTable(con, "mtcars") dbDisconnect(con) ## End(Not run)
## Not run: library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE) dbReadTable(con, "mtcars") dbDisconnect(con) ## End(Not run)
This driver is for implementing the R database (DBI) API.
This class should always be initialized with the ODBC()
function.
ODBC driver does nothing for ODBC connection. It just exists for S4 class compatibility with DBI package.
ODBC()
ODBC()
## Not run: driver <- RODBCDBI::ODBC() # Connect to a ODBC data source con <- dbConnect(driver, dsn="test") # Always cleanup by disconnecting the database #' dbDisconnect(con) ## End(Not run)
## Not run: driver <- RODBCDBI::ODBC() # Connect to a ODBC data source con <- dbConnect(driver, dsn="test") # Always cleanup by disconnecting the database #' dbDisconnect(con) ## End(Not run)
See documentation of generics for more details.
## S4 method for signature 'ODBCResult' dbGetRowCount(res, ...) ## S4 method for signature 'ODBCResult' dbGetStatement(res, ...) ## S4 method for signature 'ODBCResult' dbGetInfo(dbObj, ...) ## S4 method for signature 'ODBCResult' dbColumnInfo(res, ...)
## S4 method for signature 'ODBCResult' dbGetRowCount(res, ...) ## S4 method for signature 'ODBCResult' dbGetStatement(res, ...) ## S4 method for signature 'ODBCResult' dbGetInfo(dbObj, ...) ## S4 method for signature 'ODBCResult' dbColumnInfo(res, ...)
res |
An object of class |
... |
Ignored. Needed for compatibility with generic |
dbObj |
An object inheriting from |
## Not run: library(DBI) data(USArrests) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "t1", USArrests, overwrite=TRUE) dbWriteTable(con, "t2", USArrests, overwrite=TRUE) dbListTables(con) rs <- dbSendQuery(con, "select * from t1 where UrbanPop >= 80") dbGetStatement(rs) dbHasCompleted(rs) info <- dbGetInfo(rs) names(info) info$fields dbFetch(rs, n=2) dbHasCompleted(rs) info <- dbGetInfo(rs) info$fields dbClearResult(rs) # DBIConnection info names(dbGetInfo(con)) dbDisconnect(con) ## End(Not run)
## Not run: library(DBI) data(USArrests) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "t1", USArrests, overwrite=TRUE) dbWriteTable(con, "t2", USArrests, overwrite=TRUE) dbListTables(con) rs <- dbSendQuery(con, "select * from t1 where UrbanPop >= 80") dbGetStatement(rs) dbHasCompleted(rs) info <- dbGetInfo(rs) names(info) info$fields dbFetch(rs, n=2) dbHasCompleted(rs) info <- dbGetInfo(rs) info$fields dbClearResult(rs) # DBIConnection info names(dbGetInfo(con)) dbDisconnect(con) ## End(Not run)
Provides Access to Databases Through the ODBC Interface An implementation of R's DBI interface using ODBC package as a back-end. This allows R to connect to any DBMS that has a ODBC driver.