【问题标题】:Error while executing the query in SqlLite3在 Sqlite3 中执行查询时出错
【发布时间】:2021-05-23 22:26:45
【问题描述】:

用户在 Telegram bot 中按 /start 后,我​​正在尝试添加用户名、用户 ID 和许多其他变量。但我在这段代码中遇到错误

def add_user(userid, username):
    userCount = c.execute(f"COUNT({userIdColumn} FROM {info_table} WHERE {userIdColumn}={userid}")
    if(userCount <= 0):
        c.execute(f"INSERT INTO {info_table}({userIdColumn},{usernameColumn},{isBannedColumn},{isSubscribedColumn},{userNotesColumn}) VALUES ({userid},{username},0,1,' ')")
        c.commit()
        db.close()

我尝试在函数内声明 cursor() 甚至数据库,但我得到的错误是

  1. sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 20532 and this is thread id 19184.
  2. sqlite3.OperationalError: near "COUNT": syntax error

我也知道有一篇关于同样问题的帖子很遗憾没有解决我的问题。

我的问题是这里的正确声明是什么?

【问题讨论】:

  • 查询中的 COUNT 之前是否缺少 SELECT
  • 第一个错误'sqlite3.ProgrammingError'是由于您使用光标c的方式造成的。您可以有一个连接,然后为 CRUD 操作创建多个游标。
  • @Robin 哦,我现在看到了
  • @simpleApp 是的,我在函数内声明了光标,但我仍然有线程错误

标签: python sql database sqlite telegram-bot


【解决方案1】:

第一个错误的修复是添加 check_same_thread=False 作为参数 = sqlite3.connect("Your db",check_same_thread=False) 并且您需要在函数内声明 cursor()

第二个错误是由错误的格式引起的,应该是SELECT COUNT

【讨论】:

    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 2012-11-18
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多