我需要创建一个表名有一些特殊字符的表。我正在使用RSQLite
包。我需要创建的表名是port.3.1
。用这个名字创建表是不可能的。所以我根据What are valid table names in SQLite?将表名改为[port.3.1]
。无法将值插入到sqlite表
现在我可以创建表格,但是我无法向该表格插入数据框。 我使用的代码如下:
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]
。这背后的原因是什么?我怎么解决这个问题?
太棒了..谢谢你的时间和精力..非常有价值的信息给我......再次感谢.. :) –