使用 PYTHON 的字符串填充方式
import mysql.connector sql = 'select \* from school.student where age > {age} and address = {addr};' info = {'age' : 18, 'addr' : 'shenzhen'} # 參數是字典類型 sql = sql.format(\*\*info) mysql\_conn = mysql.connector.connect(host='host', user='user', passwd='password') cursor = conn.cursor() cursor.execute(sql)
使用 SQL 模塊中自帶的填充方式
import mysql.connector sql = 'select \* from school.student where age > %s and address = %s;' # 所有的填充字符都是 %s mysql\_conn = mysql.connector.connect(host='host', user='user', passwd='password') cursor = conn.cursor() info = (18, 'shenzhen') # 參數必須是 元組類型 cursor.execute(sql, info)