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.

Node.js: mysql2 で MariaDB のデータを更新 (Update)

Last updated at Posted at 2020-06-03

プログラム

フォルダー構造

$ 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
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?