Создал вручную базу, кодом не хочет создаваться:
import sqlite3
import telebot
bot = telebot.TeleBot("мой токен бота")
conn = sqlite3.connect('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)
Ошибка:
Traceback (most recent call last):
File "C:\Users\Desktop\test.py", line 34, in <module>
bot.polling(none_stop=True)
File "D:\lib\site-packages\telebot\__init__.py", line 620, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "D:\lib\site-packages\telebot\__init__.py", line 694, in __threaded_polling
raise e
File "D:\lib\site-packages\telebot\__init__.py", line 650, in __threaded_polling
self.worker_pool.raise_exceptions()
File "D:\lib\site-packages\telebot\util.py", line 143, in raise_exceptions
raise self.exception_info
File "D:\lib\site-packages\telebot\util.py", line 89, in run
task(*args, **kwargs)
File "C:\Users\Desktop\test.py", line 31, in get_text_messages
db_table_val(user_id=us_id, user_name=us_name, user_surname=us_sname, username=username)
File "C:\User\Desktop\test.py", line 11, in db_table_val
cursor.execute('INSERT INTO test (user_id, user_name, user_surname, username) VALUES (?, ?, ?, ?)',
sqlite3.OperationalError: no such table: test