PYTHON 中 SQL 帶參數


使用 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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM