这是迄今为止我找到的最佳解决方案。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)
}
另请参阅:
如何将字符串拆分为给定长度的子字符串?