Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
from flask import Flask, request, render_template_string, jsonify
|
| 2 |
import sqlite3
|
| 3 |
-
import requests
|
| 4 |
import os
|
| 5 |
|
| 6 |
app = Flask(__name__, template_folder="./")
|
|
@@ -14,7 +13,8 @@ def init_db():
|
|
| 14 |
CREATE TABLE IF NOT EXISTS contacts (
|
| 15 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 16 |
name TEXT NOT NULL,
|
| 17 |
-
phone TEXT NOT NULL
|
|
|
|
| 18 |
)
|
| 19 |
''')
|
| 20 |
conn.commit()
|
|
@@ -31,17 +31,18 @@ def add_contact():
|
|
| 31 |
try:
|
| 32 |
name = request.args.get('name')
|
| 33 |
phone = request.args.get('phone')
|
|
|
|
| 34 |
|
| 35 |
-
if not name or not phone:
|
| 36 |
-
return "
|
| 37 |
|
| 38 |
conn = sqlite3.connect('data.db')
|
| 39 |
cursor = conn.cursor()
|
| 40 |
-
cursor.execute('INSERT INTO contacts (name, phone) VALUES (?, ?)', (name, phone))
|
| 41 |
conn.commit()
|
| 42 |
conn.close()
|
| 43 |
|
| 44 |
-
return f"Contact added: {name} - {phone}", 200
|
| 45 |
except Exception as e:
|
| 46 |
print(f"Error adding contact: {e}")
|
| 47 |
return "Internal Server Error", 500
|
|
@@ -52,7 +53,7 @@ def show_contacts():
|
|
| 52 |
try:
|
| 53 |
conn = sqlite3.connect('data.db')
|
| 54 |
cursor = conn.cursor()
|
| 55 |
-
cursor.execute('SELECT name, phone FROM contacts')
|
| 56 |
contacts = cursor.fetchall()
|
| 57 |
conn.close()
|
| 58 |
|
|
@@ -66,7 +67,7 @@ def show_contacts():
|
|
| 66 |
<title>Contacts</title>
|
| 67 |
<style>
|
| 68 |
table {
|
| 69 |
-
width:
|
| 70 |
border-collapse: collapse;
|
| 71 |
}
|
| 72 |
th, td {
|
|
@@ -85,11 +86,13 @@ def show_contacts():
|
|
| 85 |
<tr>
|
| 86 |
<th>Name</th>
|
| 87 |
<th>Phone</th>
|
|
|
|
| 88 |
</tr>
|
| 89 |
{% for contact in contacts %}
|
| 90 |
<tr>
|
| 91 |
<td>{{ contact[0] }}</td>
|
| 92 |
<td>{{ contact[1] }}</td>
|
|
|
|
| 93 |
</tr>
|
| 94 |
{% endfor %}
|
| 95 |
</table>
|
|
|
|
| 1 |
from flask import Flask, request, render_template_string, jsonify
|
| 2 |
import sqlite3
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
app = Flask(__name__, template_folder="./")
|
|
|
|
| 13 |
CREATE TABLE IF NOT EXISTS contacts (
|
| 14 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 15 |
name TEXT NOT NULL,
|
| 16 |
+
phone TEXT NOT NULL,
|
| 17 |
+
email TEXT NOT NULL
|
| 18 |
)
|
| 19 |
''')
|
| 20 |
conn.commit()
|
|
|
|
| 31 |
try:
|
| 32 |
name = request.args.get('name')
|
| 33 |
phone = request.args.get('phone')
|
| 34 |
+
email = request.args.get('email')
|
| 35 |
|
| 36 |
+
if not name or not phone or not email:
|
| 37 |
+
return "Parameters 'name', 'phone', and 'email' are required.", 400
|
| 38 |
|
| 39 |
conn = sqlite3.connect('data.db')
|
| 40 |
cursor = conn.cursor()
|
| 41 |
+
cursor.execute('INSERT INTO contacts (name, phone, email) VALUES (?, ?, ?)', (name, phone, email))
|
| 42 |
conn.commit()
|
| 43 |
conn.close()
|
| 44 |
|
| 45 |
+
return f"Contact added: {name} - {phone} - {email}", 200
|
| 46 |
except Exception as e:
|
| 47 |
print(f"Error adding contact: {e}")
|
| 48 |
return "Internal Server Error", 500
|
|
|
|
| 53 |
try:
|
| 54 |
conn = sqlite3.connect('data.db')
|
| 55 |
cursor = conn.cursor()
|
| 56 |
+
cursor.execute('SELECT name, phone, email FROM contacts')
|
| 57 |
contacts = cursor.fetchall()
|
| 58 |
conn.close()
|
| 59 |
|
|
|
|
| 67 |
<title>Contacts</title>
|
| 68 |
<style>
|
| 69 |
table {
|
| 70 |
+
width: 70%;
|
| 71 |
border-collapse: collapse;
|
| 72 |
}
|
| 73 |
th, td {
|
|
|
|
| 86 |
<tr>
|
| 87 |
<th>Name</th>
|
| 88 |
<th>Phone</th>
|
| 89 |
+
<th>Email</th>
|
| 90 |
</tr>
|
| 91 |
{% for contact in contacts %}
|
| 92 |
<tr>
|
| 93 |
<td>{{ contact[0] }}</td>
|
| 94 |
<td>{{ contact[1] }}</td>
|
| 95 |
+
<td>{{ contact[2] }}</td>
|
| 96 |
</tr>
|
| 97 |
{% endfor %}
|
| 98 |
</table>
|