Answer the question
In order to leave comments, you need to log in
I made a database for a telegram bot and it constantly gives an error, what should I do?
Here is the code:
import telebot
import sqlite3
bot = telebot.TeleBot("2094994681:AAGCllfBnfytGnLLSrEd49kAfLOCSOaeucE")
conn = sqlite3.connect('db/database.db', check_same_thread=False)
cursor = conn.cursor()
def db_table_val(user_id: int, user_name: str, user_surname: str, username: str):
cursor.execute('INSERT INTO test (user_id, user_name, user_surname, username) VALUES (?, ?, ?, ?)', (user_id, user_name, user_surname, username))
conn.commit()
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'Добро пожаловать')
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
if message.text.lower() == 'привет':
bot.send_message(message.chat.id, 'Привет! Ваше имя добавлено в базу данных!')
us_id = message.from_user.id
us_name = message.from_user.first_name
us_sname = message.from_user.last_name
username = message.from_user.username
db_table_val(user_id=us_id, user_name=us_name, user_surname=us_sname, username=username)
bot.polling(none_stop=True)
cursor.execute('INSERT INTO test (user_id, user_name, user_surname, username) VALUES (?, ?, ?, ?)', (user_id, user_name, user_surname, username))
sqlite3.OperationalError: no such table: test
Answer the question
In order to leave comments, you need to log in
They write to you:
no such table: test
You do not have a table called "test".
At least create one.
Each message is processed in a separate thread, i.e. connection (connect) must be done in each @bot.message_handler handler
for sqlite, semaphores (fcntl.flock) will also be required, because the local DBMS does not handle locks well
lock_bd=open(os.path.join(os.path.dirname(os.path.realpath(__file__)),'db','database.lock'),'a')
fcntl.flock(lock_bd,fcntl.LOCK_EX)
conn = sqlite3.connect('db/database.db', check_same_thread=False)
cursor = conn.cursor()
....
cursor.close()
conn.close()
lock_bd.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question