我将多个参数传递给原始sql语句,对于使用%s的每个参数 是否可以使用变量名而不是多个%s
示例:
man = "John"
wife = "Jane"
query = "SELECT * FROM person WHERE name = %s and wife = %s"
cursor.execute(query, [man, wife])
答案 0 :(得分:0)
这是你要查询的模型吗?
的文档 raw() automatically maps fields in the query to fields on the model.
这应该像以下一样简单:
Person.objects.raw('SELECT id, first_name, last_name, birth_date FROM myapp_person')
答案 1 :(得分:0)
DB API 2.0定义了以下参数样式
'qmark'
问号样式,
例如'...WHERE name=?'
'numeric'
数字,位置风格,
例如'...WHERE name=:1'
'named'
命名样式,
例如'...WHERE name=:name'
'format'
ANSI C printf格式代码,
例如'...WHERE name=%s'
'pyformat'
Python扩展格式代码,
例如'...WHERE name=%(name)s'
现在,并非所有数据库都实现了所有这些数据库。检查您使用的数据库引擎是否支持pyformat
。