Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ActiveRecord::Base.connection.execute( some sql )
- require 'mysql'
- m = MySQL.new("localhost", "username", "password", "database")
- r = m.query("SELECT * FROM people ORDER BY name")
- r.each_hash do |f|
- print "#{f['name']} - #{f['email']}"
- end
- require "dbi"
- require "dbi/dbrc"
- # == Configuration
- DB = "sympa"
- HOST = "saphir"
- cnt = 0
- dup = 0
- # == Crude option processing
- #
- list_name = ARGV.shift.to_s
- file = ARGV.shift.to_s
- db = DBI::DBRC.new(DB)
- DBI.connect(db.dsn + ":#{HOST}", db.user, db.password) do |dbh|
- date = Time.now.asctime
- if not list_name or list_name == "" then
- puts "List name is mandatory"
- exit 1
- end
- req1 = <<-"EOR"
- insert into user_table (email_user,lang_user)
- values (?, ?)
- EOR
- ...
- req2 = <<-"EOR"
- insert into subscriber_table
- (user_subscriber, list_subscriber, visibility_subscriber,
- date_subscriber, reception_subscriber) values (?, ?, ?, NOW(), ?)
- EOR
- sth1 = dbh.prepare(req1)
- sth2 = dbh.prepare(req2)
- ...
- #
- # Insertion in user_table
- #
- begin
- sth1.execute(line, "en")
- cnt += 1
- rescue DBI::DatabaseError => err
- $stderr.puts("DBI: #{err}")
- end
- >> result = ActiveRecord::Base.connection.execute("SHOW TABLES")
- => #<Mysql::Result:0x37ecb30>
- >> result.class.instance_methods - Object.instance_methods
- => ["all_hashes", "field_seek", "row_tell", "fetch_field_direct", "free", "field_tell", "fetch_lengths", "num_fields", "data_seek", "fetch_row", "num_rows", "fetch_field", "each", "each_hash", "fetch_hash", "row_seek", "fetch_fields"]
Add Comment
Please, Sign In to add comment