我在我的脚本中看到了最奇怪的行为。我从 dbapi2.py 得到一个 ValueError ,它似乎正在尝试对 datetime.date 对象进行转换,就好像它是 datetime.datetime 对象一样:
File "C:\Python27\Lib\sqlite3\dbapi2.py", line 66, in convert_timestamp
datepart, timepart = val.split(" ")
ValueError: need more than 1 value to unpack
我没有做任何特别的事情,事实上我有几个其他脚本在做完全相同的事情,但 tSql 略有不同,完全没有问题。这是构建 Sql 的类:
class TransactionQuery:
def __init__(self, account_iter, date):
self.account_iter = tuple(account_list)
self.date = date
placeholder = ("?" for item in self.account_list)
placeholders = ', '.join(item for item in placeholder)
self.query = """select * from transactions where account_cd in (%s) and
effected_dt = ?""" % placeholders
@property
def params(self):
return (self.account_list) + (self.date,)
调用它的代码:
with Sqlite() as curs:
print mquery.query
print mquery.params
curs.execute(mquery.query, mquery.params)
return curs.fetchall()
以及打印语句的输出:
select * from transactions where account_cd in (?, ?, ?, ?) and effected_dt = ?
('713271', '71204', '713311', '713471', datetime.date(2012, 12, 17))
任何想法为什么 Sqlite 在这种情况下会遇到 datetime.date 对象的问题?