Skip to content

Instantly share code, notes, and snippets.

@masapico
Created February 7, 2012 11:26
Show Gist options
  • Select an option

  • Save masapico/1759228 to your computer and use it in GitHub Desktop.

Select an option

Save masapico/1759228 to your computer and use it in GitHub Desktop.
Ruby: SQLite3 insert binary
require "sqlite3"
db = SQLite3::Database.new("test.sqlite3")
sql = <<SQL
create table test(
content blob
)
SQL
db.execute_batch(sql)
binary = File.open("test.pdf", "rb").read
db.execute("insert into test (content) values(?)", SQLite3::Blob.new(binary))
res = db.execute("select content from test")
File.open("res2.pdf", "wb").write(res[0][0])
p res[0][0].size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment