我可以将什么参数传递给dbConnect? [英] What arguments can I pass to dbConnect?

查看:152
本文介绍了我可以将什么参数传递给dbConnect?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要连接到SQLite数据库,它是相当简单:您传递一个驱动程序和一个路径到SQLite文件。



对于其他 DBI - 兼容的数据库后端(PostgreSQL,MySQL,等等),您可以传递到 dbConnect 不明显,也没有记录。



如何找到我可以传递给 dbConnect 以形成连接?

解决方案

可用的参数取决于后端, code> dbConnect 只显示驱动程序和 ... 参数。



在我们继续之前,加载数据库连接所需的包( RSQLite RPostgreSQL RMySQL

要查找驱动程序的名称

>



这应该与数据库后端的名称相同。目前支持的值包括
SQLitePostgreSQL OracleJDBC



字符串必须始终是DB包中函数的名称,因此如果有疑问,请搜索包(例如使用 ls(package:RSQLite ))并猜测看起来合理的名称。






.. args



您可以使用 showMethods

 code> showMethods(dbConnect)
##功能:dbConnect(package DBI)
## drv =character
## drv =PostgreSQLConnection
## drv =PostgreSQLDriver
## drv =SQLiteConnection
## drv =SQLiteDriver

$ b b

获取适当后端的方法,在名称中使用 Driver

  getMethod(dbConnect,PostgreSQLDriver)
#输出的方法定义部分
## function(drv,...)
# #postgresqlNewConnection(drv,...)

可用的参数是函数的调用



?postgresqlNewConnection

 
##函数(drv,user =,password =,host =,dbname =,
## port =,tty = ,forceISOdate = TRUE)



为方便起见,下面是其他后端的参数:



?sqliteNewConnection

 #SQLite 
args(sqliteNewConnection)
## function(drv,dbname =,loadable.extensions = TRUE,cache_size = NULL,
## synchronous = 0,flags = NULL,vfs = NULL)

?mysqlNewConnection

 #MySQL 
args(mysqlNewConnection)
##函数(drv,dbname = NULL,username = NULL,password = NULL,host = NULL,
## unix.socket = NULL,port = 0,client.flag = 0,groups ='rs-dbi',
## default.file = NULL)

OraSupport

 #Oracle 
args(.oci.Connect)
## function ,username =,password =,dbname =,prefetch = FALSE,
## bulk_read = 1000L,stmt_cache = 0L,external_credentials = FALSE,
## sysdba = FALSE)

RJDBC 的工作方式略有不同。 dbConnect 方法接受其他命名的args,而不是调用另一个子函数。

  formalArgs(getMethod(dbConnect,JDBCDriver))
## [1]drvurluserpassword...
/ pre>

To connect to a SQLite database, it is reasonably straightforward: you pass a driver and a path to the SQLite file.

For other DBI-compatible database backends (PostgreSQL, MySQL, etc.), what you can pass to dbConnect isn't obvious, nor well documented.

How do I find out what I can pass to dbConnect to form the connection?

解决方案

The arguments available depend upon the backend, which is why dbConnect only shows driver and ... arguments.

Before we proceed, load the packages required for the database connection (RSQLite, RPostgreSQL, RMySQL or whatever).


To find the name of the driver

This should be the same as the name of the database backend. Currently supported values include "SQLite", "PostgreSQL", "MySQL", "Oracle", "JDBC".

The string must always be the name of a function in the DB package, so if in doubt search through the package (use, for example, ls("package:RSQLite")) and guess at names that look plausible.


To find the ... args

You can see the methods available for connection using showMethods.

showMethods("dbConnect")
## Function: dbConnect (package DBI)
## drv="character"
## drv="PostgreSQLConnection"
## drv="PostgreSQLDriver"
## drv="SQLiteConnection"
## drv="SQLiteDriver"

Get the method for the appropriate backend, with Driver in the name.

getMethod(dbConnect, "PostgreSQLDriver")
# The 'Method Definition' section of the output
## function (drv, ...) 
## postgresqlNewConnection(drv, ...)

The available arguments are are those of the function that is called inside the previous method definition.

?postgresqlNewConnection

args(postgresqlNewConnection)
## function (drv, user = "", password = "", host = "", dbname = "", 
##     port = "", tty = "", options = "", forceISOdate = TRUE) 

For convenience, here are the args for other backends:

?sqliteNewConnection

# SQLite
args(sqliteNewConnection)
## function (drv, dbname = "", loadable.extensions = TRUE, cache_size = NULL, 
##     synchronous = 0, flags = NULL, vfs = NULL)

?mysqlNewConnection

# MySQL
args(mysqlNewConnection)
## function(drv, dbname=NULL, username=NULL, password=NULL, host=NULL,       
##    unix.socket = NULL, port = 0, client.flag = 0, groups = 'rs-dbi', 
##    default.file = NULL)

?OraSupport

# Oracle
args(.oci.Connect)
## function(drv, username = "", password = "", dbname = "", prefetch = FALSE, 
##     bulk_read = 1000L, stmt_cache = 0L, external_credentials = FALSE, 
##     sysdba = FALSE)

RJDBC works slightly differently. Rather than calling another subfunction, its dbConnect method accepts additional named args.

formalArgs(getMethod(dbConnect, "JDBCDriver"))
## [1] "drv"      "url"      "user"     "password" "..."

这篇关于我可以将什么参数传递给dbConnect?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆