【发布时间】:2013-06-04 10:12:34
【问题描述】:
我需要创建一个表名包含一些特殊字符的表。我正在使用RSQLite 包。我需要创建的表名是port.3.1。无法使用此名称创建表。所以我在What are valid table names in SQLite?的基础上把表名改成了[port.3.1]。
现在我可以创建表了,但我无法向该表插入数据框。 我使用的代码如下:
createTable <- function(tableName){
c <- c(portDate='varchar(20) not null' ,
ticker='varchar(20)',
quantities='double(20,10)')
lite <- dbDriver("SQLite", max.con = 25)
db <- dbConnect(lite, dbname="sql.db")
if( length(which(strsplit(toString(tableName),'')[[1]]=='.') ) != 0){ tableName = paste("[",tableName,"]",sep="") } #check whether the portfolio contains special characters or not
sql <- dbBuildTableDefinition(db, tableName, NULL, field.types = c, row.names = FALSE)
print(sql)
dbGetQuery(db, sql)
}
datedPf <- data.frame(date=c("2001-01-01","2001-01-01"), ticker=c("a","b"),quantity=c(12,13))
for(port in c("port1","port2","port.3.1")){
createTable(port)
lite <- dbDriver("SQLite", max.con = 25)
db <- dbConnect(lite, dbname="sql.db")
if( length(which(strsplit(toString(port),'')[[1]]=='.') ) != 0){ port = paste("[",port,"]",sep="") } #check whether the portfolio contains special characters or not
dbWriteTable(db,port,datedPf ,append=TRUE,row.names=FALSE)
}
在这个例子中,我可以将数据框插入表port1 和port2,但它没有插入表[port.3.1]。这背后的原因是什么?我该如何解决这个问题?
【问题讨论】: