これが私がこれまでに見つけた最良の解決策です。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)
}
参照:
文字列を特定の長さの部分文字列に分割する方法