Python3: 删除 MariaDB 的数据
编程
文件夹结构
$ tree -a
.
├── .env
├── config_mariadb.py
└── maria_delete.py
.env
config_mariadb.py
在这里
Python3:显示MariaDB版本
#! /usr/bin/python
#
# maria_delete.py
#
# Feb/22/2023
#
# --------------------------------------------------------
import os
import sys
import mariadb
#
from config_mariadb import config_mariadb_proc
# --------------------------------------------------------
def sql_delete_proc(cursor_aa,key_in):
print("*** sql_delete_proc ***")
#
sql_str = sql_delete_string_gen_proc(key_in)
cursor_aa.execute(sql_str)
# --------------------------------------------------------
def sql_delete_string_gen_proc(key_in):
sql_str="delete from cities where id = '%s'" % key_in
print(sql_str)
#
return sql_str
# --------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
key_in = sys.argv[1]
print ("%s" % key_in)
#
host,user,password,data_base = config_mariadb_proc()
conn = mariadb.connect(user=user, password=password, \
host=host,database=data_base)
#
cursor = conn.cursor()
#
sql_delete_proc(cursor,key_in)
conn.commit()
#
cursor.close()
conn.close()
sys.stderr.write("*** 終了 ***\n")
#
# --------------------------------------------------------
执行结果
$ ./maria_delete.py t3327
*** 開始 ***
t3327
*** sql_delete_proc ***
delete from cities where id = 't3327'
*** 終了 ***