3

This is the simplified code for my telegram bot

import telegram.bot, telegram, sqlite3
from telegram.ext import Updater,CommandHandler,MessageHandler,Filters

class DatabaseManager(object):
    def __init__(self, db):
        self.conn = sqlite3.connect(db)
        self.conn.execute('pragma foreign_keys = on')
        self.conn.commit()
        self.cur = self.conn.cursor()

    def query(self, arg):
        self.cur.execute(arg)
        self.conn.commit()
        return self.cur

    def __del__(self):
        self.conn.close()


def regcheck(uid):
    for row in dbmgr.query("SELECT * FROM users WHERE id="+str(uid)+";"):
        print "regcheck is running" #this is never printed
        if row == "":
            return 1
        else:
            return 0

def start(bot, update):
    if regcheck(update.message.chat_id) == 1:
        bot.send_message(chat_id=update.message.chat_id, text="OK")
    else:
        bot.send_message(chat_id=update.message.chat_id,text="FAIL")

def main():
    global dbmgr
    dbmgr = DatabaseManager("usr.sql")

    for row in dbmgr.query("select * from users WHERE id="+str(uid)+";"):
        print row #this works perfectly fine

    updater = Updater(token=token)
    dispatcher = updater.dispatcher

    start_handler = CommandHandler('start', start)
    dispatcher.add_handler(start_handler)

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

I understand, that using a global variable for dbmgr is sub-optimal, but this is my first python-telegram-bot project, and I am not aware of a (better, or at least working) way to accomplish this.

Why does row in the main() get printed, but the dbmgr.query in regcheck() gets stuck?

randomcoding
  • 89
  • 1
  • 3
  • 10

1 Answers1

1

Try to change initialization of SQLite connection:

self.conn = sqlite3.connect(db, check_same_thread=False)

Read more about here:

wowkin2
  • 5,895
  • 5
  • 23
  • 66