Answer the question
In order to leave comments, you need to log in
Why does my pe variable not change and only the first user id is recorded?
Does not change to another number pe here is the code
if call.data == 'aple_1':
user_id_vv = call.from_user.id это я получаю id пользвателя что-бы в дальнейшом создать таблицу с названием какой у него id
user_id_v = 'a' + str(user_id_vv) тут просто к чифрам а прибовляю
conn = sqlite3.connect("telebot00.db") # или :memory: чтобы сохранить в RAM коннектюсь
cursor = conn.cursor()
cursor.execute(f"""INSERT INTO users_id (user_id) VALUES(?)""", всё записываеться проверял
(user_id_v,))
conn.commit()
conn.close()
con = sqlite3.connect("telebot00.db")
cur = con.cursor()
result = cur.execute("""SELECT * FROM users_id""").fetchall() result да он тоже прекрастно работает в него записываеться второй id пользователя
print(result)
# Вывод результатов на экран
count = 0
for pe in result:
print(pe) но вот сюда не записываеться 1 id аользователя только 2 дальше можно не смотреть проблема вот тут в этом цикле
peremenna_user_id = peremenna_user_id + str(pe[1])
print(peremenna_user_id)
print(mas_user_id)
if peremenna_user_id not in mas_user_id:
mas_user_id.append(str(peremenna_user_id))
print('Новый пользователь БОТА: ', mas_user_id[-1])
conn_new_table = sqlite3.connect("telebot00.db") # или :memory: чтобы сохранить в RAM
cursor_new_table = conn_new_table.cursor()
# Создание таблицы
cursor_new_table.execute("""CREATE TABLE {}
(id integer NOT NULL, class text, name_original text, name text,
user_name text, product text, phone text, PRIMARY KEY (ID)
)""".format(str(mas_user_id[-1])))
if peremenna_user_id in mas_user_id:
peremenna_user_id = ''
a = 'Яблоко'
print('Старый пользователь бота:', mas_user_id[-1])
con_v = sqlite3.connect('telebot00.db')
cur_v = con_v.cursor()
cur_v.execute(f"""INSERT INTO {user_id_v} (product) VALUES(?)""",
(a, ))
con_v.commit()
con_v.close()
Answer the question
In order to leave comments, you need to log in
Let's start with the fact that in sql it's better not to return all the values of the table at once, you can read about this in the manuals, in your case it's better to return exactly what you need. And if you answer your question. You return everything, that is, a set that stores a lot of data in itself and then iterate over it, that is, even if there is only one column in this table, the output will be like this (123, ) that is, the first value is empty, and zero stores the data you need , so most likely the error is in this line
peremenna_user_id = peremenna_user_id + str(pe[1])
This should be zero. And if I'm wrong, then I'll show you a more or less decent implementation of such code and I think you will succeed with it:
cursor.execute('SELECT id, cond, name FROM inventory')
inventory_user_data = cursor.fetchall()
print(inventory_user_data)
for data in inventory_user_data:
id, condition, name = data
if id == message.from_user.id:
bot.send_message(id, f'{name}, {condition}')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question