R连接到sqlite [英] R connection to sqlite
问题描述
无法从 R 连接到 sqlite.Sqlite3 安装在 linxu 服务器上并且能够创建/修改.但是 R 没有连接.
Unable to connect to sqlite from R. Sqlite3 is installed on the linxu server and am able to create/modify. But R isn't connecting.
library(dplyr)
library(RSQLite)
> db <- src_sqlite("my_db.sqlite3", create = TRUE)
Error in .local(drv, ...) : Could not connect to database:
unable to open database file
能够从命令行连接到sqlite
Able to connect to sqlite from command line
@ubuntu:~$ sqlite3
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
这是会话信息
会话信息()R 版本 3.2.2 (2015-08-14)平台:x86_64-pc-linux-gnu(64 位)运行于:Ubuntu 14.04.1 LTS
sessionInfo() R version 3.2.2 (2015-08-14) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 14.04.1 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RSQLite_1.0.0 DBI_0.3.1 dplyr_0.4.3
loaded via a namespace (and not attached):
[1] magrittr_1.5 R6_2.1.1 assertthat_0.1 parallel_3.2.2 tools_3.2.2
[6] Rcpp_0.12.1
>
推荐答案
SQLite 是一个文件级数据库,因此引用它需要一个完整的目录路径.没有在文件名中指定工作目录或完整路径的位置.
SQLite is a file level database, hence to reference it requires a full directory path. No where do you specify the working directory or a full path in the file name.
默认情况下,R 将使用 getwd()
中包含的当前工作目录.如果这个文件夹中没有数据库,就会出现连接错误.您可以使用 setwd()
更改工作目录.
By default, R will use the current working directory contained in getwd()
. If database is not contained in this folder, then connection error will emerge. You can change working directory with setwd()
.
顺便说一下,您引用了两个包,但使用 dplyr 包连接到 SQLite="nofollow">src_sqlite,而不是 RSQLite.
By the way, you reference both packages but are connecting to SQLite with the dplyr
package using src_sqlite, not with RSQLite.
RSQLite 连接
library(RSQLite)
setwd("/Path/To/Database/Folder")
sqlite <- dbDriver("SQLite")
conn <- dbConnect(sqlite,"my_db.sqlite3")
DPLYR 连接
library(dplyr)
setwd("/Path/To/Database/Folder")
db <- src_sqlite("my_db.sqlite3", create = TRUE)
您可能不想同时调用这两个库以避免同名函数发生冲突.
You might not want to call both libraries together to avoid conflict of same named functions.
这篇关于R连接到sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!