關於django原生數據庫LIKE加參數的使用。


導入模塊

from django.db import connection

contact=request.POST.get('contact')
year=request.POST.get('year')
month=request.POST.get('month')
filterTime="%%"+year+'-'+month+"%%"

param=(contact,filterTime)
print(param)
#currentTime=datetime.datetime(int(year),int(month))
cursor=connection.cursor()
sql='SELECT score,COUNT(1) FROM `webapp_commentsys` WHERE contact=%s and create_time LIKE %s GROUP BY score;'
print(sql)
cursor.execute(sql,param)
records=cursor.fetchall()
注意事項
1.在django中使用LIKE"%參數%"改為LIKE"%%參數%%"
2.LIKE中的參數不要直接寫在sql里,如寫成如上filterTime="%%"+year+'-'+month+"%%"帶進去否則可能不是你想要的。


免責聲明!

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



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