【问题标题】:how to check all rows for coincidence(sqlite)如何检查所有行的巧合(sqlite)
【发布时间】:2021-08-02 14:53:21
【问题描述】:

我需要通过检查sql db中的所有id行来检查用户是否已经注册,怎么做?

如果我这样做:

cursor = conn.execute("SELECT ID from USERS")
for row in cursor:
        if row[0] == message.from_user.id:
        bot.send_message(message.chat.id, 'already registed')

他只检查第一行

【问题讨论】:

    标签: python sql sqlite


    【解决方案1】:

    我不确定为什么您的代码没有检查所有行,这就是 for row in cursor: 应该做的。

    但是使用WHERE 子句只搜索您想要的特定行会更有效。

    cursor = conn.execute("SELECT COUNT(*) from USERS WHERE id = ?", (message.from_user.id,))
    row = cursor.fetchone()
    if row[0] > 0:
        bot.send_message(message.chat.id, 'already registered')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 2021-08-03
      • 2020-03-14
      • 2021-09-27
      • 2021-06-17
      • 1970-01-01
      • 2021-08-16
      相关资源
      最近更新 更多