我想通过传递两个参数来创建一个连接到SQLite数据库的函数:数据库名称和表。
我试过了:
sqLiteConnect <- function(database, table) {
con <- dbConnect("SQLite", dbname = database)
query <- dbSendQuery(con, "SELECT * FROM ", table)
fetch(query, n = -1)
}
但我通过了result <- sqLiteConnect(primary_database, "table_name")
我得到了Error in sqliteExecStatement(conn, statement, ...) : RS-DBI driver: (error in statement: near " ": syntax error)
如果我将我的功能改为
sqLiteConnect <- function(database, table) {
con <- dbConnect("SQLite", dbname = database)
query <- dbSendQuery(con, "SELECT * FROM ", table, "")
fetch(query, n = -1)
}
我得到Error in sqliteExecStatement(conn, statement, ...) : unused argument ("")
我猜问题是将变量连接到字符串。
答案 0 :(得分:1)
dbSendQuery要求将SQL语句作为单个字符串(它不将表作为参数),因此您需要使用paste()或sprintf()创建它,例如:
sqLiteConnect <- function(database, table) {
con <- dbConnect("SQLite", dbname = database)
query <- dbSendQuery(con, paste("SELECT * FROM ", table, ";", sep=""))
fetch(query, n = -1)
}