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("[email protected]", "abc11111")
# Update 更新
statement = client.prepare("update users set email = ? where id= ?")
statement.execute("[email protected]", 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