ruby mysql blob

Recently, one of mycert’s internal project required that PDF files to be saved into the database (MyQL). Since its is not easy to find the sample code via Google, here’s a quick note for future reference.

Ruby

1

2

3

4

fContent=File.open("/path/file","wb").read()

db=Mysql.new('localhost','user','password','database')

st=db.prepare("insert into tableA( fieldBlob) values (?)")

st.execute(fContent)

Another thing is that the max file size for blob is 64k. To store more than that, one need to use ‘longblob’. Not sure what is the max size for this type. Otherwise, your binary just been truncated by mysql without any warning (I spend quite some time debugging the code before realizing this. Happy trying!