ruby DBI安装使用指南
2023-03-09 22:26:19 时间
1.安装
首先,我是使用mysql进行测试的,你的机器上需要安装mysql数据库。
然后执行:
gem install mysql
到rubyforge下载ruby-DBI,解压后cd到目录运行如下命令:
ruby setup.rb config --with=dbi,dbd_mysql
ruby setup.rb setup
ruby setup.rb install
完整的setup命令参数参考DBI的doc
2.完整例子
DBI是一类似于ODBC的开发式的统一的数据库编程接口,结构层次上可以分为两层:
1.Database Interface——数据库接口层,与数据库无关,提供与数据库无关的标准接口
2.Database Driver——数据库驱动,与数据库相关
DBI也是很简单易用的,一个完整的使用例子,对于初学者可能有点帮助:
首先,我是使用mysql进行测试的,你的机器上需要安装mysql数据库。
然后执行:
gem install mysql
到rubyforge下载ruby-DBI,解压后cd到目录运行如下命令:
ruby setup.rb config --with=dbi,dbd_mysql
ruby setup.rb setup
ruby setup.rb install
完整的setup命令参数参考DBI的doc
2.完整例子
DBI是一类似于ODBC的开发式的统一的数据库编程接口,结构层次上可以分为两层:
1.Database Interface——数据库接口层,与数据库无关,提供与数据库无关的标准接口
2.Database Driver——数据库驱动,与数据库相关
DBI也是很简单易用的,一个完整的使用例子,对于初学者可能有点帮助:
require 'dbi'
begin
#连接数据库
dbh=DBI.connect("DBI:Mysql:dbi_test:localhost","root","")
dbh.columns("simple").each do |h|
p h
end
#示范3种事务处理方式
#手动commit
dbh["AutoCommit"]=false
1.upto(10) do |i|
sql = "insert into simple (name, author) VALUES (?, ?)"
dbh.do(sql, "Song #{i}", "#{i}")
end
dbh.commit
#使用transaction方法
dbh.transaction do |dbh|
1.upto(10) do |i|
sql = "insert into simple (name, author) VALUES (?, ?)"
dbh.do(sql, "Song #{i}", "#{i}")
end
end
#使用SQL语句
dbh.do("SET AUTOCOMMIT=0")
dbh.do("BEGIN")
dbh["AutoCommit"]=false
dbh.do("UPDATE simple set name='test' where id='1'")
dbh.do("COMMIT")
#查询
sth=dbh.execute("select count(id) from simple")
puts "bookCount:#{sth.fetch[0]}"
sth.finish
begin
sth=dbh.prepare("select * from simple")
sth.execute
while row=sth.fetch do
p row
end
sth.finish
rescue
end
#上面这段查询可以改写为:
#dbh.select_all("select * from simple") do |row|
# p row
#end
#使用工具类输出xml格式结果集以及测量查询时间
sql="select * from simple"
mesuretime=DBI::Utils::measure do
sth=dbh.execute(sql)
end
puts "SQL:#{sql}"
puts "Time:#{mesuretime}"
rows=sth.fetch_all
col_names=sth.column_names
sth.finish
puts DBI::Utils::XMLFormatter.table(rows)
dbh.do("delete from simple")
rescue DBI::DatabaseError=>e
puts "error code:#{e.err}"
puts "Error message:#{e.errstr}"
ensure
dbh.disconnect if dbh
end
begin
#连接数据库
dbh=DBI.connect("DBI:Mysql:dbi_test:localhost","root","")
dbh.columns("simple").each do |h|
p h
end
#示范3种事务处理方式
#手动commit
dbh["AutoCommit"]=false
1.upto(10) do |i|
sql = "insert into simple (name, author) VALUES (?, ?)"
dbh.do(sql, "Song #{i}", "#{i}")
end
dbh.commit
#使用transaction方法
dbh.transaction do |dbh|
1.upto(10) do |i|
sql = "insert into simple (name, author) VALUES (?, ?)"
dbh.do(sql, "Song #{i}", "#{i}")
end
end
#使用SQL语句
dbh.do("SET AUTOCOMMIT=0")
dbh.do("BEGIN")
dbh["AutoCommit"]=false
dbh.do("UPDATE simple set name='test' where id='1'")
dbh.do("COMMIT")
#查询
sth=dbh.execute("select count(id) from simple")
puts "bookCount:#{sth.fetch[0]}"
sth.finish
begin
sth=dbh.prepare("select * from simple")
sth.execute
while row=sth.fetch do
p row
end
sth.finish
rescue
end
#上面这段查询可以改写为:
#dbh.select_all("select * from simple") do |row|
# p row
#end
#使用工具类输出xml格式结果集以及测量查询时间
sql="select * from simple"
mesuretime=DBI::Utils::measure do
sth=dbh.execute(sql)
end
puts "SQL:#{sql}"
puts "Time:#{mesuretime}"
rows=sth.fetch_all
col_names=sth.column_names
sth.finish
puts DBI::Utils::XMLFormatter.table(rows)
dbh.do("delete from simple")
rescue DBI::DatabaseError=>e
puts "error code:#{e.err}"
puts "Error message:#{e.errstr}"
ensure
dbh.disconnect if dbh
end
文章转自庄周梦蝶 ,原文发布时间5.17
相关文章
- 数据孤岛是业务效率的无声杀手
- 2023展望:新的一年将给大数据分析领域带来什么?
- 阿里云ADB基于Hudi构建Lakehouse的实践
- 大数据在医疗保健领域的使用案例
- 微软增加说明:KB5021751 更新扫描已经 / 即将过时 Office 过程中不会触碰用户隐私
- 2022 Gartner全球云数据库管理系统魔力象限发布 腾讯云数据库入选
- 场景化、重实操,分享一个实时数仓实践案例
- Arctic的湖仓一体践行之路
- 分布式计算MapReduce究竟是怎么一回事?
- 淘系数据模型治理优秀实践
- 大数据分析对医疗保健的影响
- 当我们说大数据Hadoop,究竟在说什么?
- 2022年及以后大数据的五个发展趋势
- 网易严选离线数仓治理实践
- 2023 年数据治理趋势
- 一份“靠谱”的年度经营计划,你学会了吗?
- 漫谈对大数据的思考
- 测试一下,读懂数据的能力,你有吗?
- 用艺术的眼光探索数据之美
- 聊聊数据分析成果如何落地