I add command /admin /vadmin /iadmin /hello I create database and I improve code
It's version 0.3.0
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
DIR = "/Users/mac/myfirstprogramm/storage/message.db"
|
||||
if __name__ == "__main__":
|
||||
db = sqlite3.connect(DIR)
|
||||
|
||||
cursor = db.cursor()
|
||||
|
||||
# cursor.execute("""CREATE TABLE message (
|
||||
# chat_id integer,
|
||||
# message_id integer
|
||||
# )""")
|
||||
# db.commit()
|
||||
|
||||
cursor.execute("SELECT * FROM message")
|
||||
print(cursor.fetchone())
|
||||
|
||||
db.close()
|
||||
|
||||
|
||||
|
||||
def get_db():
|
||||
return sqlite3.connect(DIR)
|
||||
|
||||
|
||||
+20
-14
@@ -1,23 +1,29 @@
|
||||
import os
|
||||
from .DB import get_db
|
||||
|
||||
MESSAGES_FILE = "storage/message.txt"
|
||||
|
||||
# --- функция для записи message_id ---
|
||||
def save_message(chat_id: int, message_id: int):
|
||||
with open(MESSAGES_FILE, "a", encoding="utf-8") as f:
|
||||
f.write(f"{chat_id},{message_id}\n")
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
cur.execute("INSERT INTO message VALUES (?, ?)", (int(chat_id), int(message_id)))
|
||||
db.commit()
|
||||
cur.close()
|
||||
db.close()
|
||||
|
||||
# --- функция для загрузки всех сообщений ---
|
||||
def load_messages():
|
||||
if not os.path.exists(MESSAGES_FILE):
|
||||
return []
|
||||
with open(MESSAGES_FILE, "r", encoding="utf-8") as f:
|
||||
lines = f.readlines()
|
||||
return [tuple(map(int, line.strip().split(","))) for line in lines if line.strip()]
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
cur.execute("SELECT * FROM message")
|
||||
rows = cur.fetchall()
|
||||
cur.close()
|
||||
db.close()
|
||||
return rows
|
||||
|
||||
# --- функция для очистки файла ---
|
||||
def clear_messages():
|
||||
open(MESSAGES_FILE, "w").close()
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
cur.execute("DELETE FROM message")
|
||||
db.commit()
|
||||
cur.close()
|
||||
db.close()
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user