ruby 操作mysql crud

ruby的语法真的是太优雅了!require mysql2# 配置数据源,连接musqlclient = Mysql2::Client.new(host: localhost,username: root,password: root,database: test,encoding: utf8)# Insert 插入state...

ruby的语法真的是太优雅了!

require 'mysql2'

# 配置数据源,连接musql
client = Mysql2::Client.new(
  host: 'localhost',
  username: 'root',
  password: 'root',
  database: 'test',
  encoding: 'utf8'
)

# Insert 插入
statement = client.prepare("insert into users (email, password) values (?, ?)")
statement.execute("adley123@kk.com", "abc11111")

# Update 更新
statement = client.prepare("update users set email = ? where id= ?")
statement.execute("axs@122.com", 120)

# Delete 删除
statement = client.prepare("delete from users where id = ?")
statement.execute(114)

# Query 查询
results = client.query('SELECT * FROM users')
results.each do |row|
  puts row
end

puts results.count

本文标题为:ruby 操作mysql crud

基础教程推荐