【发布时间】:2015-02-10 15:43:55
【问题描述】:
我正在使用 Django 1.7。
我正在尝试实现搜索功能。输入搜索词时,我需要在数据库中搜索该词的所有表和所有列(我只有 7 个表,总共可能有 40 列,数据库不是很大)。我使用 MySQL 作为数据库。
我可以查询1个表,所有列的代码如下
query = Q(term__contains=tt) | Q(portal__contains=tt) | ......so on
data = ABC.objects.filter(query)
我试过用UNION,写个SQL就好了
select * from table A where col1 like %s OR col2 like %s .....
UNION
select * from table B where col1 like %s OR col2 like %s .....
当我尝试像下面这样实现时,我收到了一个错误“格式字符串的参数不足”
cursor = connection.cursor()
cursor.execute("select * from table A where col1 like %s OR col2 like %s
UNION
select * from table B where col1 like %s OR col2 like %s", tt)
那么如何为多个变量传递参数(即使在这种情况下它们是相同的)?我也尝试过多次。
谢谢。
【问题讨论】:
-
你可能想看看django-watson
标签: python django django-1.7