Untitled
unknown
plain_text
2 years ago
1.4 kB
4
Indexable
from flask import Flask, render_template, url_for, request, g
import sqlite3
DATABASE = 'my_database.db'
app = Flask(__name__)
connection = sqlite3.connect('my_database.db')
cursor = connection.cursor()
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
return db
cursor.execute('''
CREATE TABLE IF NOT EXISTS Comments (
username TEXT NOT NULL,
comment TEXT NOT NULL
)
''')
if input("1 - Записать нового пользователя\n2 - Удалить определённого пользователя\nВвод: ") == '1':
username = input("Введите имя: ")
text = input("Введите текст: ")
print(f"{username} - {text}")
cursor.execute(f'INSERT INTO Comments (username, comment) VALUES ("{username}", "{text}")')
data = cursor.fetchall()
else:
username = input("Введите имя пользователя, которое хоттите удалить: ")
cursor.execute(f'DELETE FROM Comments WHERE username = "{username}"')
cursor.execute(f'SELECT * FROM Comments')
print(cursor.fetchall())
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
if __name__ == "__main__":
app.run(debug=True)
Editor is loading...
Leave a Comment