0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ruby: MariaDB のデータを読む (Read)

Posted at

プログラム

フォルダー構造

$ 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
*** 終了 ***
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?