python - django中使用SQL原生语句如何动态插入变量
本文介绍了python - django中使用SQL原生语句如何动态插入变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
我用python写了一段代码用于批量插入数据库出错,如何解决,伪代码:
from django.db import connection
...
cursor = connection.cursor()
cursor.execute("Insert into api_coupon(Quan_surplus,Quan_receive,) values ("+d['Quan_surplus']+","+d['Quan_receive']+") on duplicate key update Quan_receive="+d['Quan_receive']+",Quan_surplus="+d['Quan_surplus'])
cursor.close()
...
请教下
1、以上sql语句中变量的拼接是否正确,如果不正确该如何拼接
2、错误提示TypeError: coercing to Unicode: need string or buffer, int found ,请问如何解决~!
解决方案
用%s格式化query语句
这篇关于python - django中使用SQL原生语句如何动态插入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文