Python3: 显示MariaDB的版本
程式
文件夹结构
$ tree -a
.
├── .env
├── config_mariadb.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")
#
# --------------------------------------------------------
#! /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
# --------------------------------------------------------
host = 'localhost'
user = 'scott'
password = 'tiger123'
data_base = 'city'
执行结果
$ ./server_version.py
*** 開始 ***
10.11.2-MariaDB
*** 終了 ***
请参阅此页面。
如何将Python程序连接到MariaDB