我想在单击submit按钮后将表单保存到db browser sqlite上。数据库名为pettycash.db。表名为submitclaim。我已经在数据库中创建了相应的字段。我需要在insert语句中使用字段名还是在request.form['name']等中使用名称。
应用程序类型
@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()
提交目标.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>
1条答案
按热度按时间sh7euo9m1#
您需要在sqlite数据库中创建表。在创建表时,请提及所需的列。
在插入数据时,需要使用表的列名。
有关更多详细信息,请参阅本教程https://likegeeks.com/python-sqlite3-tutorial/.