Update app.py
Browse files
app.py
CHANGED
|
@@ -39,9 +39,11 @@ if not os.path.exists(UPLOAD_FOLDER):
|
|
| 39 |
|
| 40 |
|
| 41 |
# Создание базы данных и таблицы
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
-
conn = sqlite3.connect(
|
| 45 |
cursor = conn.cursor()
|
| 46 |
cursor.execute('''
|
| 47 |
CREATE TABLE IF NOT EXISTS contacts (
|
|
@@ -54,10 +56,10 @@ def init_db():
|
|
| 54 |
conn.commit()
|
| 55 |
conn.close()
|
| 56 |
except Exception as e:
|
| 57 |
-
print(f"Error initializing database: {e}")
|
| 58 |
|
| 59 |
-
|
| 60 |
-
init_db()
|
| 61 |
|
| 62 |
|
| 63 |
|
|
@@ -172,7 +174,7 @@ def add_contact():
|
|
| 172 |
if not name or not phone or not email:
|
| 173 |
return "Parameters 'name', 'phone', and 'email' are required.", 400
|
| 174 |
|
| 175 |
-
conn = sqlite3.connect('
|
| 176 |
cursor = conn.cursor()
|
| 177 |
cursor.execute('INSERT INTO contacts (name, phone, email) VALUES (?, ?, ?)', (name, phone, email))
|
| 178 |
conn.commit()
|
|
@@ -183,17 +185,15 @@ def add_contact():
|
|
| 183 |
print(f"Error adding contact: {e}")
|
| 184 |
return "Internal Server Error", 500
|
| 185 |
|
| 186 |
-
# Маршрут для отображения таблицы контактов из gc
|
| 187 |
@app.route('/contacts')
|
| 188 |
def show_contacts():
|
| 189 |
try:
|
| 190 |
-
conn = sqlite3.connect('
|
| 191 |
cursor = conn.cursor()
|
| 192 |
cursor.execute('SELECT name, phone, email FROM contacts')
|
| 193 |
contacts = cursor.fetchall()
|
| 194 |
conn.close()
|
| 195 |
|
| 196 |
-
# HTML-шаблон для отображения таблицы
|
| 197 |
html = '''
|
| 198 |
<!doctype html>
|
| 199 |
<html lang="en">
|
|
@@ -250,7 +250,6 @@ def show_contacts():
|
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
-
|
| 254 |
# Переменные с данными
|
| 255 |
|
| 256 |
action_d = "add"
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
# Создание базы данных и таблицы
|
| 42 |
+
DATABASES = ['data1.db', 'data2.db', 'data3.db', 'data4.db', 'data5.db']
|
| 43 |
+
|
| 44 |
+
def init_db(db_name):
|
| 45 |
try:
|
| 46 |
+
conn = sqlite3.connect(db_name)
|
| 47 |
cursor = conn.cursor()
|
| 48 |
cursor.execute('''
|
| 49 |
CREATE TABLE IF NOT EXISTS contacts (
|
|
|
|
| 56 |
conn.commit()
|
| 57 |
conn.close()
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f"Error initializing database {db_name}: {e}")
|
| 60 |
|
| 61 |
+
for db in DATABASES:
|
| 62 |
+
init_db(db)
|
| 63 |
|
| 64 |
|
| 65 |
|
|
|
|
| 174 |
if not name or not phone or not email:
|
| 175 |
return "Parameters 'name', 'phone', and 'email' are required.", 400
|
| 176 |
|
| 177 |
+
conn = sqlite3.connect('data1.db')
|
| 178 |
cursor = conn.cursor()
|
| 179 |
cursor.execute('INSERT INTO contacts (name, phone, email) VALUES (?, ?, ?)', (name, phone, email))
|
| 180 |
conn.commit()
|
|
|
|
| 185 |
print(f"Error adding contact: {e}")
|
| 186 |
return "Internal Server Error", 500
|
| 187 |
|
|
|
|
| 188 |
@app.route('/contacts')
|
| 189 |
def show_contacts():
|
| 190 |
try:
|
| 191 |
+
conn = sqlite3.connect('data1.db')
|
| 192 |
cursor = conn.cursor()
|
| 193 |
cursor.execute('SELECT name, phone, email FROM contacts')
|
| 194 |
contacts = cursor.fetchall()
|
| 195 |
conn.close()
|
| 196 |
|
|
|
|
| 197 |
html = '''
|
| 198 |
<!doctype html>
|
| 199 |
<html lang="en">
|
|
|
|
| 250 |
|
| 251 |
|
| 252 |
|
|
|
|
| 253 |
# Переменные с данными
|
| 254 |
|
| 255 |
action_d = "add"
|