N
N
Nika9802022-01-06 17:29:04
Python
Nika980, 2022-01-06 17:29:04

Python does not see the Sqlite table, what should I do?

According to the video tutorial, HowdyHo tried to connect the database to the telegram bot, did everything as he did in the video, but the error "Error no such table: users"
pops up. I go into the tables and see the "users" table with my own eyes, but the python does not see.
What to do with it?

import sqlite3
import telebot
import random
from telebot import types

try:
    conn = sqlite3.connect("Database.db")
    cursor = conn.cursor()

    cursor.execute("INSERT OR IGNORE INTO 'users' ('user_id') VALUES (?)", (1000,))
    users = cursor.execute("SELECT * FROM 'users'")
    print(users.fetchall())

    conn.commit()

except sqlite3.Error as error:
    print("Error", error)

finally:
    if(conn):
        conn.close()
bot = telebot.TeleBot('TOKEN')
@bot.message_handler(content_types=['text'])
def get_text_messages(message):
        bot.send_message(message.from_user.id, f"Привет, {message.from_user.first_name}")
        markup = types.InlineKeyboardMarkup()
        button1 = types.InlineKeyboardButton('МОСКВА', callback_data='1')
        button2 = types.InlineKeyboardButton('ЛЮБЕРЦЫ', callback_data='2')
        button3 = types.InlineKeyboardButton('КОРОЛЕВ', callback_data='3')
        markup.row(button1, button2)
        markup.row(button3)
        bot.send_message(message.from_user.id, f"Выберите город", reply_markup=markup)
@bot.callback_query_handler(func=lambda call: True)
def query_handler(call):
    bot.answer_callback_query(callback_query_id=call.id)
    
    answer = ''
    if call.data == '1':
        markup = types.InlineKeyboardMarkup()
        button23 = types.InlineKeyboardButton('Набор наклеек v1', callback_data='23')
        button24 = types.InlineKeyboardButton('Набор наклеек v2', callback_data='24')
        button25 = types.InlineKeyboardButton('Набор наклеек v3', callback_data='25')
        markup.row(button23)
        markup.row(button24)
        markup.row(button25)
        bot.send_message(call.message.chat.id, f"Ваше предпочтение", reply_markup=markup)
        bot.answer_callback_query(callback_query_id=call.id) 
        answer = ''
bot.polling(none_stop=True, interval=0)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-06
@Vindicar

video tutorial HowdyHo

Well, this is actually the problem. These videos explain almost nothing, but just "do as I do." You need to learn the language properly, bots are not the most trivial thing.
In general, the galaxy is above the rights. You specify the database like this: sqlite3.connect("Database.db")
If you don't remember, this is what is called a " relative path ", which is relative to the current working directory. The problem is that the current working directory when the script is run can be different - it is not necessarily the directory where the script is located (although it often can be). There are many factors that affect the current working directory and it is best not to rely on it.
It is safer to find out the full path to the script directory, and use it to build the path to the script's working files. For example, like this:
import sys
import pathlib
script_path = pathlib.Path(sys.argv[0]).parent  # абсолютный путь до каталога, где лежит скрипт
conn = sqlite3.connect(script_path / "Database.db")  # формируем абсолютный путь до файла базы

After that, you can be sure which database the bot is accessing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question