這裏是最好的我迄今發現的解決方案。 DbDownloadImages函數需要很短的時間來執行(實際上幾乎沒有時間)。
# Helper function I use
Paste <- function(string, vals)
{
string <- gsub(x = string, pattern = '\\?', replacement = "%s")
result <- do.call(sprintf, as.list(c(string,vals)))
return(result)
}
# conn is a RMySQL connection object
DbInsertImage <- function(conn, link.to.file)
{
binary = readBin (link.to.file , what = "raw", n = 1e6)
binary.str = paste (binary, collapse = "")
statement = Paste ("CALL InsertImage('?')" , c(binary.str))
dbSendQuery(conn, statement)
return(GetIdentity(conn)) # one of my helper functions ;
# it returns the "LAST INSERT ID"
}
#conn is a RMySQL connection object
DbDownloadImage <- function(conn, id, destination)
{
query = "SELECT Data FROM Image WHERE Id = ? LIMIT 1"
query = Paste(query, c(id))
result = dbGetQuery(conn, query)$Data[1]
sst <- strsplit(result, "")[[1]]
result <- paste0(sst[c(TRUE, FALSE)], sst[c(FALSE, TRUE)])
result <- as.raw (as.hexmode (result))
f = file (destination, "wb")
writeBin(object = result, con = f)
close(f)
}
另見: How to split a string into substrings of a given length?
嘿,這部分地回答我的問題;現在唯一的問題是我需要它是一個字符串,以便將它作爲BLOB插入到MySQL中; :-( – MadSeb 2012-07-23 17:39:20