Issue
I want to save the form onto DB Browser Sqlite once I click on the submit button. The database is called pettycash.db. The table name is SubmitClaim. I've created the respective fields in the db. Do I need to use the field names in the INSERT STATEMENT or use the names that I use in request.form['name'] etc.
App.py
@app.route('/addrec', methods=['POST', 'GET'])
def addrec():
if request.method == 'POST':
name = request.form['name']
depart = request.form['depart']
type = request.form['type']
uploadre = request.form['uploadre']
amt = request.form['amt']
price = request.form['price']
description = request.form['description']
conn = sql.connect('pettycash.db')
c = conn.cursor()
c.execute(
"INSERT INTO SubmitClaim VALUES (?,?,?,?,?,?,?)", (name, depart, type, uploadre, amt, price, description))
conn.commit()
c.execute("SELECT * FROM SubmitClaim")
print(c.fetchall())
conn.close()
Submitclaim.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
</head>
<body>
<form action="{{ url_for('addrec') }}" method="POST">
<!-- Enter name -->
<div class="row">
<div class="arrange1">
<label for style="font-size:20px" > Enter your name: </label>
{{ user.get("name") }}
</div>
</div>
<!-- Enter email -->
<div class="row">
<div class="arrange1">
<label for style="font-size:20px"> Enter your email: </label>
{{ user.get("email") }}
</div>
</div>
<!-- Select department-->
<div class="dropdown">
<label for>Choose your department: </label>
<select name="depart" id="depart">
<option value="mgt">MGT</option>
<option value="com">COM</option>
<option value="ctd">CTD</option>
<option value="fin">FIN</option>
<option value="hrd">HRD</option>
<option value="ita">ITA</option>
<option value="ldd">LDD</option>
<option value="pjd">PJD</option>
<option value="qsd">QSD</option>
</select>
</div>
<!-- Select type of claim -->
<div class="dropdown">
<label for>Choose type of claim: </label>
<select name="type" id="type">
<option value="transport">Transport Expenses</option>
<option value="utlity">Utility Expenses</option>
<option value="office">Office Supplies</option>
<option value="refresh">Refreshments</option>
</select>
</div>
<!-- Enter claim amount -->
<div class="arrange1">
<label for style="font-size:20px"> Enter claim amount: </label>
<input type="text" class="name-input" name="amt" autofocus="true" />
</div>
<!-- Upload Receipt -->
<div class="arrange1">
<label for style="font-size:20px">Upload Receipt: </label>
<input type="file" class="file-input" name="uploadre" placeholder="name" autofocus="true" />
</div>
<!-- Tick checkbox if amount more than 1000 -->
<div class="arrange2">
<input type="checkbox" id="price" name="price" value="amount">
<label for="price"> For amount > 1000 </label><br>
</div>
<!-- Enter claim details -->
<div class="arrange1">
<label for style="font-size:20px"> Enter claim description: </label>
<textarea rows="3" name="description" cols="30"></textarea>
</div>
<!-- Submit claim -->
<div class="arrange3">
<button type="submit" class="submit-button" name="save", value="save">Submit</button>
</div>
</form>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
Solution
You need to create table in sqlite database. Mention columns whatever you require while creating the table.
While inserting the data, you need to use the column names of Table.
For more details follow this tutorial https://likegeeks.com/python-sqlite3-tutorial/.
Answered By - Avinash G Bhosale
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.