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.

Python3: MariaDB のバージョンを表示

Last updated at Posted at 2023-02-22

プログラム

フォルダー構造

$ tree -a
.
├── .env
├── config_mariadb.py
└── server_version.py
server_version.py
#! /usr/bin/python
#
#	server_version.py
#
#					Feb/22/2023
# --------------------------------------------------------
import	mariadb
import	sys
#
from config_mariadb import config_mariadb_proc
# --------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
host,user,password,data_base = config_mariadb_proc()
conn = mariadb.connect(user=user, password=password, \
                              host=host,database=data_base)
#
cursor = conn.cursor ()
#
cursor.execute ("SELECT VERSION ()")
#
row = cursor.fetchone ()
print (row[0])
#
cursor.close ()
conn.close ()
#
sys.stderr.write("*** 終了 ***\n")
#
# --------------------------------------------------------
config_mariadb.py
#! /usr/bin/python
#
#	config_mariadb.py
#
#					Feb/22/2023
# --------------------------------------------------------
import os
from dotenv import load_dotenv
#
# --------------------------------------------------------
def config_mariadb_proc():
	dotenv_path = '.env'
	load_dotenv(dotenv_path)
	host = os.environ.get("host")
	user = os.environ.get("user")
	password = os.environ.get("password")
	data_base = os.environ.get("data_base")
#
	return	host,user,password,data_base
# --------------------------------------------------------
.env
host = 'localhost'
user = 'scott'
password = 'tiger123'
data_base = 'city'

実行結果

$ ./server_version.py 
*** 開始 ***
10.11.2-MariaDB
*** 終了 ***

参考ページ

How to connect Python programs to MariaDB

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?