Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
import sqlite3
# Создаём базу,
# если её ещё не существует
with sqlite3.connect("tg.sqlite") as conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users
(CHAT_ID INTEGER, name TEXT)
""")
# Теперь запишем:
chat_id = 123 # допустим
text = "Vasya"
with sqlite3.connect("tg.sqlite") as conn:
cursor.execute("INSERT INTO users VALUES (?,?)", (chat_id, text))
conn.commit()
# И теперь прочитаем.
chat_id = 123 # допустим
# У нас есть только chat_id,
# получим имя из нашей базы:
with sqlite3.connect("tg.sqlite") as conn:
sql = "SELECT name FROM users WHERE chat_id=?"
cursor.execute(sql, [(chat_id)])
name = cursor.fetchone()[0]
print(f"Пользователя с айди {chat_id} зовут {name}.")
# всё!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question