# -*- coding: utf-8 -*- """快速执行 SQL 并打印结果:python q.py "select ..." "select ..." """ import re import sys import pymysql from _root import ROOT YML = str(ROOT / "yoshop-java/src/main/resources/application.yml") pwd = re.findall(r"password:\s*(.+)", open(YML, encoding="utf-8").read())[0].strip().strip("\"'") conn = pymysql.connect(host="192.168.1.177", port=3306, user="root", password=pwd, database="shop", autocommit=True) cur = conn.cursor() for sql in sys.argv[1:]: cur.execute(sql) if cur.description is None: print(f"OK ({cur.rowcount} rows)") print("---") continue cols = [d[0] for d in cur.description] print(" | ".join(cols)) for r in cur.fetchall(): print(" | ".join(str(x) for x in r)) print("---") conn.close()