Skip to content

Commit c846ae1

Browse files
authored
Add files via upload
1 parent 4339f2b commit c846ae1

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

PythonProject/app.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from flask import Flask, request, render_template, redirect, url_for
2+
import mysql.connector as ms
3+
4+
app = Flask(__name__)
5+
6+
# Create a connection to the database
7+
def get_db_connection():
8+
print('to be Connected....')
9+
try:
10+
conn = ms.connect(user='root',password='pwd',host='localhost',database='python01')
11+
print('Connected....')
12+
return conn
13+
except ms.Error as err:
14+
print('Error:',err)
15+
return None
16+
17+
18+
19+
# Initialize the database
20+
def init_db():
21+
conn = get_db_connection()
22+
mycursor = conn.cursor()
23+
mycursor.execute('select * from users;')
24+
res=mycursor.fetchall()
25+
for user in res:
26+
print(user)
27+
28+
conn.close()
29+
30+
init_db()
31+
32+
# Route for the home page with the form
33+
@app.route('/', methods=('GET', 'POST'))
34+
def index():
35+
print("esfawsf",request.form)
36+
print("method", request.method)
37+
if request.method == 'POST':
38+
name = request.form['name']
39+
email = request.form['email']
40+
#dob = request.form['dateofbirth']
41+
dob='2024-10-16'
42+
43+
44+
45+
46+
47+
# Insert the data into the database
48+
conn = get_db_connection()
49+
mycursor = conn.cursor()
50+
51+
52+
mycursor.execute("SELECT count(username) from users where username='"+name+"'")
53+
x=mycursor.fetchone()
54+
55+
print("count=",x[0])
56+
if(x[0]>=1):
57+
print("user already exists")
58+
return redirect(url_for('already'))
59+
60+
61+
62+
63+
mycursor.execute("INSERT INTO users(username, email,dob,status) VALUES (%s,%s, %s,'Active')", (name, email,dob))
64+
conn.commit()
65+
mycursor.close()
66+
# conn.commit()
67+
conn.close()
68+
69+
return redirect(url_for('userlist'))
70+
else:
71+
return render_template('index.html')
72+
73+
74+
# Route to display success message
75+
@app.route('/success')
76+
def success():
77+
return 'Data successfully submitted!'
78+
79+
80+
81+
@app.route('/already')
82+
def already():
83+
return 'user already exists!'
84+
85+
86+
# Route to display success message
87+
@app.route('/userlist')
88+
def userlist():
89+
conn = get_db_connection()
90+
mycursor = conn.cursor()
91+
mycursor.execute('select * from users;')
92+
users = mycursor.fetchall()
93+
94+
mycursor.close()
95+
# conn.commit()
96+
conn.close()
97+
return render_template('userlist.html',users=users)
98+
99+
if __name__ == '__main__':
100+
app.run()

PythonProject/form.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from flask import Flask, render_template, request
2+
import sqlite3
3+
4+
app = Flask(__name__)
5+
6+
# Database setup (creates the table if it doesn't exist)
7+
conn = sqlite3.connect('mydatabase.db') # Replace 'mydatabase.db' with your desired name
8+
cursor = conn.cursor()
9+
cursor.execute('''
10+
CREATE TABLE IF NOT EXISTS entries (
11+
id INTEGER PRIMARY KEY AUTOINCREMENT,
12+
name TEXT,
13+
email TEXT
14+
)
15+
''')
16+
conn.commit()
17+
conn.close()
18+
19+
20+
@app.route('/', methods=['GET', 'POST'])
21+
def index():
22+
if request.method == 'POST':
23+
name = request.form['name']
24+
email = request.form['email']
25+
26+
conn = sqlite3.connect('mydatabase.db')
27+
cursor = conn.cursor()
28+
cursor.execute("INSERT INTO entries (name, email) VALUES (?, ?)", (name, email))
29+
conn.commit()
30+
conn.close()
31+
32+
return "Data submitted successfully!" # You might want a more sophisticated response
33+
34+
return render_template('form.html')
35+
36+
37+
if __name__ == '__main__':
38+
app.run(debug=True)

PythonProject/main.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This is a sample Python script.
2+
3+
# Press Shift+F10 to execute it or replace it with your code.
4+
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
5+
6+
7+
def print_hi(name):
8+
# Use a breakpoint in the code line below to debug your script.
9+
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
10+
11+
12+
# Press the green button in the gutter to run the script.
13+
if __name__ == '__main__':
14+
print_hi('PyCharm')
15+
16+
# See PyCharm help at https://www.jetbrains.com/help/pycharm/

PythonProject/templates/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Submit Form</title>
6+
</head>
7+
<body>
8+
<h1>Submit Your Information</h1>
9+
<form method="POST" action="{{ url_for('index') }}">
10+
11+
<label for="name">Name:</label><br>
12+
<input type="text" id="name" name="name" required><br>
13+
<label for="email">Email:</label><br>
14+
<input type="email" id="email" name="email"><br><br>
15+
16+
<button type="submit"> submit it</button>
17+
18+
<!-- <input type="submit" value="Submit">-->
19+
</form>
20+
</body>
21+
</html>

PythonProject/templates/userlist.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<body>
4+
<table border="1">
5+
<tr>
6+
<th>Name</th>
7+
<th>Emmail</th>
8+
<th>Date of Birth</th>
9+
<th>Status</th>
10+
</tr>
11+
{% for user in users %}
12+
<tr>
13+
<td>{{user[1]}}</td>
14+
<td>{{user[5]}}</td>
15+
<td>{{user[3]}}</td>
16+
<td>{{user[4]}}</td>
17+
</tr>
18+
{% endfor %}
19+
20+
21+
</table>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)