37 lines
939 B
Python
37 lines
939 B
Python
import sqlite3
|
|
|
|
DIR = "/Users/mac/myfirstprogramm/storage/message.db"
|
|
|
|
if __name__ == "__main__":
|
|
db = sqlite3.connect(DIR)
|
|
cursor = db.cursor()
|
|
|
|
# создаём таблицы (лучше добавить IF NOT EXISTS)
|
|
cursor.execute("""CREATE TABLE IF NOT EXISTS message (
|
|
chat_id INTEGER,
|
|
message_id INTEGER
|
|
)""")
|
|
|
|
cursor.execute("""CREATE TABLE IF NOT EXISTS users (
|
|
user_id INTEGER,
|
|
user_group TEXT
|
|
)""")
|
|
|
|
# добавим тестовые данные
|
|
cursor.execute("INSERT INTO message VALUES (?, ?)", (1, 100))
|
|
cursor.execute("INSERT INTO users VALUES (?, ?)", (42, 'admin'))
|
|
db.commit()
|
|
|
|
# читаем данные
|
|
cursor.execute("SELECT * FROM message")
|
|
print("Message:", cursor.fetchall())
|
|
|
|
cursor.execute("SELECT * FROM users")
|
|
print("Users:", cursor.fetchall())
|
|
|
|
db.close()
|
|
|
|
|
|
def get_db():
|
|
return sqlite3.connect(DIR)
|