1.Inception库
Inception是一个开源系统,每个人或者每个公司都可以自由使用, 可以用于审核SQL语句的合法性。
Inception使用说明: https://inception-document.readthedocs.io/zh_CN/latest/
从教程中得知,如果要校验DDL语句的正确性,只需要如下的语句即可完成:
/*--user={username};--password={password};--host={host};--check=1;--port={port};*/
inception_magic_start;
use DB名;
DDL语句
inception_magic_commit;
2.Python脚本
想要通过python校验Mysql的语句,只要通过python的一些mysql库,连接到具体的数据库之后执行语句就可以了。不逼逼,代码如下:
import pymysql
import prettytable as pt
def check_sql_by_inception(host, port, username, password, db_name, ddl):
sql = build_sql(host, port, username, password, db_name, ddl)
result = run_sql(sql)
return result
def build_sql(host, port, username, password, db_name, ddl):
start_inception_sql = '''inception_magic_start;
use {};'''.format(db_name)
commit_inception_sql = '''inception_magic_commit;'''
db_connect_sql = "/*--user={username};--password={password};--host={host};--check=1;--port={port};*/".format(username=username,
password=password, host=host, port=port)
return db_connect_sql + start_inception_sql + ddl + commit_inception_sql
def run_sql(sql):
tb = pt.PrettyTable()
# 此处填入相应的部署了Inception服务的服务器地址
conn = pymysql.connect(host=, user='', passwd='',
db='', port=, charset="utf8mb4")
cur = conn.cursor()
cur.execute(sql)
result = cur.fetchall()
desc = cur.description
cur.close()
conn.close()
tb.field_names = [i[0] for i in cur.description]
for row in result:
tb.add_row(row)
print(tb)
return tb