Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc846ae1

Browse files
authored
Add files via upload
1 parent4339f2b commitc846ae1

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+
fromflaskimportFlask,request,render_template,redirect,url_for
2+
importmysql.connectorasms
3+
4+
app=Flask(__name__)
5+
6+
# Create a connection to the database
7+
defget_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+
returnconn
13+
exceptms.Erroraserr:
14+
print('Error:',err)
15+
returnNone
16+
17+
18+
19+
# Initialize the database
20+
definit_db():
21+
conn=get_db_connection()
22+
mycursor=conn.cursor()
23+
mycursor.execute('select * from users;')
24+
res=mycursor.fetchall()
25+
foruserinres:
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+
defindex():
35+
print("esfawsf",request.form)
36+
print("method",request.method)
37+
ifrequest.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+
returnredirect(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+
returnredirect(url_for('userlist'))
70+
else:
71+
returnrender_template('index.html')
72+
73+
74+
# Route to display success message
75+
@app.route('/success')
76+
defsuccess():
77+
return'Data successfully submitted!'
78+
79+
80+
81+
@app.route('/already')
82+
defalready():
83+
return'user already exists!'
84+
85+
86+
# Route to display success message
87+
@app.route('/userlist')
88+
defuserlist():
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+
returnrender_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+
fromflaskimportFlask,render_template,request
2+
importsqlite3
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+
defindex():
22+
ifrequest.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+
returnrender_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+
defprint_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+
<metacharset="utf-8">
5+
<title>Submit Form</title>
6+
</head>
7+
<body>
8+
<h1>Submit Your Information</h1>
9+
<formmethod="POST"action="{{ url_for('index') }}">
10+
11+
<labelfor="name">Name:</label><br>
12+
<inputtype="text"id="name"name="name"required><br>
13+
<labelfor="email">Email:</label><br>
14+
<inputtype="email"id="email"name="email"><br><br>
15+
16+
<buttontype="submit"> submit it</button>
17+
18+
<!-- <input type="submit" value="Submit">-->
19+
</form>
20+
</body>
21+
</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+
<tableborder="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)

[8]ページ先頭

©2009-2025 Movatter.jp