I have the following Ruby code:
94 def open_connection(info)
95 self.log "opening #{info.inspect}"
96 db = Mysql.init
97 db.options(Mysql::SET_CHARSET_NAME, 'utf8')
98 db.real_connect(info.host, info.user, info.password, info.dbname, info.port)
99 db.query("SET NAMES utf8")
100 »···res = db.query("SELECT realname FROM profiles")
101
102 »···conn = PGconn.connect("localhost", 5432, '', '', "dbname", "user", "pwd")
How do I take the res variable and loop through it to insert it into my Postgres database?
The MySQL
querymethod gives you aMysql::Resultinstance so you can useeachto get each row as an Array:If you had more columns in the result set then
each_hashmight be easier to work with, that calls the block with a Hash which maps column names to their values.To insert data into PostgreSQL, use
exec:Please use placeholders rather than interpolation, there’s no need to party like PHP programmers in 1999.
You could also use
prepareandexec_prepared:The
prepareapproach is better if you’re going to be executing the same SQL over and over again.Combining them gets the job done: