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 のデータを作成 (Create)

Posted at

プログラム

フォルダー構造

$ tree -a
.
├── .env
├── config_mariadb.rb
└── maria_create.rb

.env
config_mariadb.rb
はこちら
Ruby: MariaDB のバージョンを表示

maria_create.rb
#! /usr/bin/ruby
#
#	ruby/create/maria_create.rb
#
#					Feb/24/2023
# ---------------------------------------------------------------------
require 'mysql2'
load 'config_mariadb.rb'
#
# -------------------------------------------------------------
def dict_append_proc (dict_aa,id,name,population,date_mod)
	unit = {}
	unit['name'] = name
	unit['population'] = population
	unit['date_mod'] = date_mod
	key = id.to_s
	dict_aa[key] = unit
	return dict_aa
end
# ---------------------------------------------------------------------
def prepare_data_proc ()
dict_aa = {}
dict_aa=dict_append_proc(dict_aa,'t3321',"岡山",729638,"2006-9-18")
dict_aa=dict_append_proc(dict_aa,'t3322',"倉敷",318472,"2006-2-25")
dict_aa=dict_append_proc(dict_aa,'t3323',"津山",182564,"2006-8-8")
dict_aa=dict_append_proc(dict_aa,'t3324',"玉野",572948,"2006-11-15")
dict_aa=dict_append_proc(dict_aa,'t3325',"笠岡",893257,"2006-7-21")
dict_aa=dict_append_proc(dict_aa,'t3326',"井原",167589,"2006-9-7")
dict_aa=dict_append_proc(dict_aa,'t3327',"総社",241637,"2006-4-8")
dict_aa=dict_append_proc(dict_aa,'t3328',"高梁",438129,"2006-10-12")
dict_aa=dict_append_proc(dict_aa,'t3329',"新見",519472,"2006-6-9")
	return	dict_aa
end
# ---------------------------------------------------------------------
def	create_proc (client)
	sql_str="create TABLE cities (" \
		+ "id varchar(10) NOT NULL PRIMARY KEY," \
		+ "name varchar(20)," \
		+ "population int," \
		+ "date_mod varchar(40))"
	client.query(sql_str)
end
# ------------------------------------------------------------
def	drop_proc (client)
	sql_str="drop table cities"
	client.query(sql_str)
end
# ---------------------------------------------------------------------
def	insert_proc (client,id,name,population,date_mod)
	sql_str="INSERT into cities " \
		+ "(id, Name, Population, date_mod) values \
		('#{id}', '#{name}',#{population},'#{date_mod}')"
	client.query(sql_str)
end
# ---------------------------------------------------------------------
STDERR.puts	"*** 開始 ***"
#
host,user,password,data_base = config_mariadb_proc()
client = Mysql2::Client.new(host: host, username: user, password: password, database: data_base)
#
dict_aa=prepare_data_proc()
#
drop_proc(client)
create_proc(client)
dict_aa.each {|key,value |
	insert_proc(client,key,value['name'], \
		value['population'],value['date_mod'])
	}
#
client.close
#
STDERR.puts	"*** 終了 ***"
#
# ---------------------------------------------------------------------

実行結果

$ ./maria_create.rb 
*** 開始 ***
*** 終了 ***
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?