当前位置: 首页 > 工具软件 > Ruby/DBI > 使用案例 >

ruby dbi mysql_ruby dbi 操作mysql乱码问题

和飞翔
2023-12-01

我在windows下用netbeans编写ruby程序,想往数据库中插入一些数据。代码如下

require 'rubygems'

gem 'dbd-mysql'

require 'mysql'

require 'iconv'

begin

# connect to the MySQL server

dbh = Mysql.real_connect("localhost", "root", "root","enroll", 3306) #连接数据库本机:用户名:root 密码:sa 数据库:makedish 端口$1�706

# get server version string and display it

@stu_name="测试"

puts @stu_name

sql2="insert into candidate (candidate_name,id_card_no,candidate_no,state,has_submitted,the_year) VALUES ('"+@stu_name.to_s+"','123','123','A','Y','2011')"

puts sql2

dbh.query(sql2)

res = dbh.query("select * from candidate")

res.each do |ha|

p ha

end

rescue Mysql::Error => e

puts "An error occurred"

puts "Error code: #{e}"

puts "Error message: #{e.errstr}"

ensure

# disconnect from server

dbh.close if dbh

end

采用HeidiSQL查看Mysql数据,发现写进去的中文全是乱码

不知道有什么好的解决办法吗

 类似资料: