How can I read a localy stored file and insert it into a mysal blob
field?
Thanx
How can I read a localy stored file and insert it into a mysal blob
field?
Thanx
Jochen K. wrote:
How can I read a localy stored file and insert it into a mysal blob
field?
require ‘mysql’
begin
dbh = Mysql.real_connect(‘localhost’, ‘username’, ‘passwd’,
‘database’)
data = File.read(‘data.txt’) #get the data out of the file
data = dbh.escape_string(data) #escape any quotes in the data
dbh.query("
INSERT INTO table_name (char_field, int_field, tinyblob_field)
VALUES
(‘apple’, 3, ‘#{data}’)
")
result = dbh.query(“SELECT * FROM table_name”)
while row = result.fetch_row do
printf “%s, %s, %s\n”, row[0], row[1], row[2]
end
result.free
rescue Mysql::Error => e
puts “Error code: #{e.errno}”
puts “Error message: #{e.error}”
puts “Error SQLSTATE: #{e.sqlstate}” if e.respond_to?(‘sqlstate’)
ensure
dbh.close if dbh
end
7stud – wrote:
Jochen K. wrote:
How can I read a localy stored file and insert it into a mysal blob
field?
Just to be clear:
I am assuming ‘mysl’ and ‘mysal’ should be ‘mysql’.
The field names char_field, int_field, tinyblob_field in the
following code:
dbh.query("
INSERT INTO table_name (char_field, int_field, tinyblob_field)
VALUES
(‘apple’, 3, ‘#{data}’)
")
have no significance–those names should be whatever you named your
fields in your database, e.g. name, id, data.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs