プログラム
フォルダー構造
$ tree -a
.
├── .env
├── config_mariadb.js
├── go_update.sh
└── maria_update.js
Async/Await を使います。
maria_update.js
#! /usr/bin/node
// ---------------------------------------------------------------
// maria_update.js
//
// Feb/24/2023
//
// ---------------------------------------------------------------
'use strict'
var mysql = require('mysql2')
var config_mariadb = require('./config_mariadb')
// ---------------------------------------------------------------
function get_current_date_proc ()
{
const today = new Date ()
var ddx = (1900 + today.getYear ()) + "-" + (today.getMonth () +1)
ddx += "-" + today.getDate ()
return ddx
}
// ---------------------------------------------------------------
function update_command_gen (id_in,population_in)
{
const today = get_current_date_proc()
var command = "update cities set population = " + population_in
command += " , date_mod = '" + today + "'"
command += " where id = '" + id_in + "'"
console.log (command)
return command
}
// ---------------------------------------------------------------
async function main(id_in,population_in)
{
const [host, user, password, data_base] = config_mariadb.config_mariadb_proc()
var conn = await mysql.createConnection ({
host: host,
user: user,
password: password,
database: data_base
})
const command = update_command_gen (id_in,population_in)
await conn.execute(command)
conn.end()
console.error ("*** 終了 ***")
}
// ---------------------------------------------------------------
console.error ("*** 開始 ***")
const id_in = process.argv[2]
const population_in = process.argv[3]
console.log (id_in + "\t" + population_in)
main(id_in,population_in)
// ---------------------------------------------------------------
.env
config_mariadb.py
はこちら
Node.js: MariaDB のバージョンを表示
実行コマンド
go_update.sh
export NODE_PATH=/usr/lib/node_modules
./maria_update.js t3326 721539800
確認したバージョン
$ node -v
v19.7.0