2013-04-09 63 views
1

在R中,我使用以下函數,該函數在該函數中使用3或4個數據庫操作。但是,錯誤消息顯示如下:數據庫被鎖在R

Error in sqliteExecStatement(conn, statement, ...) : 
    RS-DBI driver: (RS_SQLite_exec: could not execute1: database is locked) 

我需要在我的代碼中做什麼修改?我的代碼如下:

library('RSQLite') 
test <- function(portfolio,date,frame){ 
    lite <- dbDriver("SQLite", max.con = 25) 
    db <- dbConnect(lite, dbname = "portfolioInfo1.db") 
    sql <- paste("SELECT * from ", portfolio," where portDate='", date, "' ", sep = "") 
    res <- dbSendQuery(db, sql) 
    data <- fetch(res) 
    frame1 <- data.frame(portDate=date,frame) 

lite <- dbDriver("SQLite", max.con = 25) 
db <- dbConnect(lite, dbname = "portfolioInfo1.db") 
    sql <- paste("delete from ", portfolio," where portDate='", date, "' ", sep = "") 
res <- dbSendQuery(db, sql) 

    lite <- dbDriver("SQLite", max.con = 25) 
    db <- dbConnect(lite, dbname = "portfolioInfo1.db") 
    dbWriteTable(db,portfolio,frame1,append=TRUE,row.names=FALSE) 
} 
tick <- c("AAPL","TH","YHOO") 
quant <- c("121","1313","131313131") 
frame <-data.frame(ticker=tick,quantities=quant) 
#print(frame) 
test("RUSEG","2006-02-28",frame) 

回答

5

看來,您連接多次而不斷開相同的數據庫。如果建立連接以阻止其他人編輯已編輯的數據庫,數據庫可能會進入鎖定狀態。

要麼在每次連接後斷開連接,要麼簡單地連接一次,執行所有查詢,並最終斷開連接。