terça-feira, 31 de julho de 2012

Ruby + Mysql + OSX com MAMP

Após ter desistido de utilizar Ruby como linguagem de script no OSX, pois não conseguia instalar o driver de conexão ao banco.


Building native extensions.  This could take a while...
ERROR:  Error installing dbd-mysql:
ERROR: Failed to build gem native extension.
      [....]
checking for mysql_ssl_set()... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  Blá, blá, blá, blá, blá....


Solução:

1 - Instalar Macports https://distfiles.macports.org/MacPorts/
2 - # port install mysql5-devel

3 - # gem install dbi
3 - # gem install dbd-mysql


And that's it


#!/usr/bin/ruby -w
# simple.rb - simple MySQL script using Ruby DBI module
require "dbi" 
begin
  # connect to the MySQL server
  # dbh = DBI.connect("DBI:Mysql:host=192.168.1.102;port=8889;database=federico", "root", "root")
  con = DBI.connect("DBI:Mysql:federico:127.0.0.1", "root", "root")
  # get server version string and display it
  row = con.select_one("SELECT VERSION()")
  puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
  puts "An error occurred"
  puts "Error code: #{e.err}"
  puts "Error message: #{e.errstr}"
ensure
  # disconnect from server
  con.disconnect if con
end


e agora é só matar a saudades desta maravilhosa liguagem ;)