# -*- coding: utf-8 -*-
require 'sqlite3'
require 'base64'
if File.exist?('abc.db')
File.delete('abc.db')
end
db = SQLite3::Database.new 'abc.db'
rows = db.execute <<-SQL
create table scenes(
id integer primary key,
name varchar(30),
image blob
);
SQL
image = File.open( 't.jpg' ,'rb').read
db.execute( "INSERT INTO scenes (image) VALUES " +
"('#{ Base64::encode64( image ) }');" )
db.execute( "SELECT * FROM scenes" ) do |scene|
image = Base64::decode64( scene[2] )
File.open( "back.png", "wb" ) do |back_out|
back_out << image
end
end
#一晚功夫...
下面是小改动,result以hash返回数据
# -*- coding: utf-8 -*-
require 'sqlite3'
require 'base64'
if File.exist?('abc.db')
File.delete('abc.db')
end
db = SQLite3::Database.new 'abc.db'
rows = db.execute <<-SQL
create table scenes(
id integer primary key,
name varchar(30),
image blob
);
SQL
image = File.open( 't.jpg' ,'rb').read
db.execute( "INSERT INTO scenes (image) VALUES " +
"('#{ Base64::encode64( image ) }');" )
db.results_as_hash = true
db.execute( "SELECT * FROM scenes" ) do |scene|
image = Base64::decode64(scene['image'].to_s )
File.open( "back.png", "wb" ) do |back_out|
back_out << image
end
end