プログラム
フォルダー構造
$ tree -a
.
├── .env
├── config_mariadb.rb
└── maria_read.rb
.env
config_mariadb.rb
はこちら
Ruby: MariaDB のバージョンを表示
maria_read.rb
#! /usr/bin/ruby
#
# maria_read.rb
#
# Feb/24/2023
#
# ------------------------------------------------------------------
require 'mysql2'
#
load 'config_mariadb.rb'
# ------------------------------------------------------------------
STDERR.puts "*** 開始 ***"
#
host,user,password,data_base = config_mariadb_proc()
client = Mysql2::Client.new(host: host, username: user, password: password, database: data_base)
sql_str = "SELECT id,name,population,date_mod FROM cities order by ID"
begin
result = client.query(sql_str)
result.each do |row|
print "#{row['id']}\t#{row['name']}\t#{row['population']}\t#{row['date_mod']}\n"
end
end
#
client.close
#
STDERR.puts "*** 終了 ***"
# ------------------------------------------------------------------
実行結果
$ ./maria_read.rb
*** 開始 ***
t3321 岡山 729638 2006-9-18
t3322 倉敷 318472 2006-2-25
t3323 津山 182564 2006-8-8
t3324 玉野 572948 2006-11-15
t3325 笠岡 893257 2006-7-21
t3326 井原 167589 2006-9-7
t3327 総社 241637 2006-4-8
t3328 高梁 438129 2006-10-12
t3329 新見 519472 2006-6-9
*** 終了 ***