LoginSignup
0
0

More than 1 year has passed since last update.

Node.js: mysql2 で MariaDB のデータを読む (Read)

Last updated at Posted at 2020-06-03

プログラム

フォルダー構造

$ tree -a
.
├── .env
├── config_mariadb.js
├── go_read.sh
└── maria_read.js

Async/Await を使います。

maria_read.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	maria_read.js
//
//					Feb/24/2024
//
// ---------------------------------------------------------------
'use strict'
var mysql = require('mysql2/promise')

var config_mariadb = require('./config_mariadb')
// ---------------------------------------------------------------
function compare_by_key_proc (left,right)
{
	var aa = left.key
	var bb = right.key

	var rvalue = 0

	if (aa < bb)
		{
		rvalue = -1
		}
	else if (aa > bb)
		{
		rvalue = 1
		}

	return	rvalue
}

// ---------------------------------------------------------------
function sort_key_proc (dict_aa)
{
	var array = new Array()

	for(var it in dict_aa)
		{
		array.push({'key':String (it), 'value':dict_aa[it]})
		}

	array.sort (compare_by_key_proc)

	return array
}

// ---------------------------------------------------------------
function dict_display_proc (dict_aa)
{
	const array_aa = sort_key_proc (dict_aa)

	array_aa.forEach (function(unit_aa,index)
		{
		const key = unit_aa.key
		const value = unit_aa.value

		var out_str = key + "\t"
		out_str += value["name"] + "\t"
		out_str += value["population"] + "\t"
		out_str += value["date_mod"]
		console.log (out_str)
		})	
}

// ---------------------------------------------------------------
async function read01 (mysql,host,user,password,data_base)
{
	try {
		var conn = await mysql.createConnection({
			host: host,
			user: user,
			password: password,
			database: data_base
			})
		const sql_str = 'select * from cities'
		const [rows, fields] = await conn.execute(sql_str)

		return rows
	} catch (ee) {
		console.log(ee)
		return ''
	} finally {
		if (conn && conn.connection) {
		conn.end()
		}
	}

}

// ---------------------------------------------------------------
async function main()
{
	const [host, user, password, data_base] = config_mariadb.config_mariadb_proc()
	const rows = await read01 (mysql,host,user,password,data_base)

	var dict_aa = new Object ()

	rows.forEach(function(row)
		{
		dict_aa[row.id] = {"name": row.name,
			"population": row.population,
			"date_mod": row.date_mod}
		})

	dict_display_proc (dict_aa)

	console.error ("*** 終了 ***")
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")
main()
// ---------------------------------------------------------------

.env
config_mariadb.py
はこちら
Node.js: MariaDB のバージョンを表示

実行コマンド

go_read.sh
export NODE_PATH=/usr/lib/node_modules
./maria_read.js

確認したバージョン

$ node -v
v19.7.0
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