我有管理指挥部
from django.db import connection
from django.core.management.base import BaseCommand
class Command(BaseCommand):
args = "[id]"
def handle(self, id, **options):
ids = []
if '-' in id:
splitted = id.split('-')
for n in range(int(splitted[0]), int(splitted[1])+1):
ids.append(n)
else:
ids.append(id)
c = connection.cursor()
c.execute('SELECT content FROM log WHERE id IN %s', [ids])
我可以通过输入命令名并指定paramater(id)来运行它
当我运行command 200-205
时,for循环返回一个列表:
ids =[200, 201, 202, 203, 204, 205]
并将其放入查询中:
SELECT content FROM log WHERE id IN (200, 201, 202, 203, 204, 205)
布莱尔詹特!你知道吗
当我运行command 200
时,id将是:
ids = [200]
执行查询时返回错误:
(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))' at line 1")
似乎SELECT content FROM log WHERE id IN (200)
没有正常工作。你知道吗
我做错什么了?你知道吗
你是不是少了一些括号?你知道吗
与
我觉得你的代码还可以,但我对Django库不太熟悉。两个(也许是显而易见的)解决方法:(1)构造两个不同的SQL语句来执行;在一个ID的情况下,它是一个简单的
SELECT content FROM log WHERE id =
(2)同样对于一个ID的情况,用所需ID的两个实例创建一个数组,这样Django就构造了这个SQL:SELECT content FROM log WHERE id IN (200, 200)
。换句话说相关问题 更多 >
编程相关推荐