Con esta configuración se acepta utf-8, tildes, 'ñ', etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require "rubygems" | |
require "mysql" | |
begin | |
#con = Mysql.new 'localhost', 'user', 'password', 'library' | |
con = Mysql.init | |
con.options(Mysql::SET_CHARSET_NAME, 'utf8') | |
con.real_connect('localhost', 'user', 'password', 'library') | |
con.query("SET NAMES utf8") | |
rs = con.query("SELECT * FROM Books") | |
n_rows = rs.num_rows | |
puts "There are #{n_rows} rows in the result set" | |
n_rows.times do | |
puts rs.fetch_row.join("\s") | |
end | |
# prepare statement | |
pst = con.prepare "INSERT INTO Books (title, isbn, desc) VALUES (?, ?, ?, ?)" | |
pst.execute("A Song of Ice and Fire", "0-553-10663-5", "Lorem ipsum est laborum") | |
rescue Mysql::Error => e | |
puts e.errno | |
puts e.error | |
ensure | |
con.close if con | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'jdbc/mysql' | |
require 'java' | |
Jdbc::MySQL.load_driver(:require) if Jdbc::MySQL.respond_to?(:load_driver) | |
mydb = "library" | |
userurl = "jdbc:mysql://localhost/#{mydb}?characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes" | |
con = java.sql.DriverManager.get_connection(userurl, 'user', 'password') | |
stmt = con.create_statement | |
stmt.execute_query("SET NAMES 'utf8'") | |
stmt.execute_query("SET CHARACTER SET 'utf8'") | |
# same as java | |
rs = stmt.execute_query("SELECT * FROM Books WHERE id=1") | |
rs.next | |
p rs.getString "title" | |
p rs.getString 3 | |
stmt.execute("INSERT INTO Books (title, isbn, desc) | |
VALUES ('A Storm of Swords', '0-553-10663-4', 'Lorem ipsum dolor sit')" |
Caracteres especiales deben ser escapados