T
T
thevalakas2017-05-28 12:20:59
Python
thevalakas, 2017-05-28 12:20:59

Python sqlite, why is there an error?

I send requests to the sqlite database and the following error occurs
Traceback (most recent call last):
File "C:/Users/Arseniy/PycharmProjects/RouletteBot/bot.py", line 24, in
bot.polling(none_stop=True)
File "C :\Users\Arseniy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telebot\__init__.py", line 244, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "C: \Users\Arseniy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telebot\__init__.py", line 268, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\Arseniy \AppData\Local\Programs\Python\Python36-32\lib\site-packages\telebot\util.py", line 103, in raise_exceptions
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
File "C:\Users\Arseniy\AppData\Local\Programs\Python\Python36-32\lib\site- packages\six.py", line 686, in reraise
raise value
File "C:\Users\Arseniy\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telebot\util.py", line 54 , in run
task(*args, **kwargs)
File "C:/Users/Arseniy/PycharmProjects/RouletteBot/bot.py", line 21, in send_welcome
user_register(message.from_user.id)
File "C:/Users/ Arseniy/PycharmProjects/RouletteBot/bot.py", line 14, in user_register
result = db.query('SELECT * FROM users WHERE tele_id=?', [(user_id)])
File "C:\Users\Arseniy\PycharmProjects\ RouletteBot\db.py",line 9, in query
self.cur.execute(sql, args) sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that
same thread.The object was created in thread id 6620 and this is thread id 9420

import config
import msg
import sqlite3
from db import db
import telebot

db = db('users.db')

bot = telebot.TeleBot(config.TOKEN)

def user_register(user_id):
     user_id = str(user_id)

     result = db.query('SELECT * FROM users WHERE tele_id=?', [(user_id)])

     if len(result.fetchall()) == 0:
          db.query('INSERT INTO users (tele_id) VALUES (?)', [(user_id)])

@bot.message_handler(commands=['start'])
def send_welcome(message):
     user_register(message.from_user.id)
     bot.send_message(message.chat.id, msg.MSG_WELCOME)

bot.polling(none_stop=True)

Connected class code
import sqlite3

class db:
    def __init__(self, db):
        self.con = sqlite3.connect(db)
        self.cur = self.con.cursor()

    def query(self, sql, args):
        self.cur.execute(sql, args)
        self.con.commit()
        return self.cur

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2017-05-28
@thevalakas

telebotuses threading , functions with a decorator message_handlerare called from a thread on its threadpool. A cursor sqlite3created in the constructor dbfrom the main thread cannot be used in a send_welcome-> user_register-> call db.queryfrom another thread.
Move cursor creation cur = self.con.cursor()into a method queryand make it a local variable.
Or cache the generated cursors in threading.local if performance is critical at that point (I don't think this is the case, based on the bot's tasks).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question