开发者社区> 问答> 正文

使用sqlite3 python进行条件查询

我使用PyTelegramBotAPI包并使用@ bot.callback_query_handler()来处理来自用户的回调查询。然后,我创建了一个函数,使用以下代码打印出该特定用户的数据库中的最后一个条目:

def splitbill(user):

row = c.execute('SELECT * FROM database WHERE user = (?) ORDER BY datetime DESC LIMIT 1', user).fetchall()
print(row[0])

返回并返回错误,指出ValueError:参数属于不受支持的类型

def splitbill(user):

row = c.execute('SELECT * FROM database WHERE user = (?) ORDER BY datetime DESC LIMIT 1', (user,)).fetchall()
print(row[0])

展开
收起
一码平川MACHEL 2019-01-22 10:18:09 9512 0
2 条回答
写回答
取消 提交回答
  • r'SELECT * FROM database WHERE user = (?) ORDER BY datetime DESC LIMIT 1'

    'SELECT * FROM database WHERE user = {} ORDER BY datetime DESC LIMIT 1'.format(user)

    2019-11-22 14:03:31
    赞同 展开评论 打赏
  • 有效的原因(user,)是因为该execute方法期望一个元组,当你(user)没有逗号传递时,python只是将其解释为user。

    你可以在python shell中快速验证这个:

    a = 'howdy'
    tuple = (a, ) # note the comma
    not_a_tuple = (a) # note the lack of comma

    print(tuple)
    ('howdy',)
    print(not_a_tuple)

    howdy

    type(tuple)

    type(not_a_tuple)

    2019-07-17 23:26:08
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载