這是初學者試圖用燒瓶將數據插入到mysql表中。我可以在表格中輸入數據,但不會插入數據。我使用pythonanywhere,所以該錯誤信息是不明確......Python燒瓶從表格插入數據
Transactions.html:
<h1>Enter a Transaction</h1>
<form action="/transactions" method=post>
<dl>
<dt>Sale Item:
<dd><input type=text name="Item" required/>
<dt>Shack:
<dd><input type=text name="Shack" required/>
<dt>Reference:
<dd><input type=text name="Paym_Reference" required/>
<dt>Amount:
<dd><input type=text name="Amount" required/>
</dl)
<br>
<p><input type=submit value=Enter>
</form>
的MySQL:transactions
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| trans_id | int(11) | NO | PRI | NULL | auto_increment |
| Item | varchar(20) | YES | | NULL | |
| Shack | varchar(10) | YES | | NULL | |
| Paym_Reference | varchar(20) | YES | | NULL | |
| Amount | int(10) | YES | | NULL | |
+----------------+-------------+------+-----+---------+----------------+
代碼:
import MySQLdb as my
conn = my.connect ('hello.mysql.pythonanywhere-services.com','hello','password','SIIL$Transactions')
c = conn.cursor()
@app.route('/add_data', methods=['GET', 'POST'])
def add_data():
Item= request.form('Item')
Shack= request.form('Shack')
Paym_Reference= request.form('Paym_Reference')
Amount= request.form('Amount')
c.execute("INSERT INTO transactions(Item, Shack, Paym_Reference, Amount) VALUES ('',%s,%s,%s,%s)")
conn.commit()
return 'Done'
@app.route('/transactions', methods=['GET', 'POST'])
def transactions():
add_data()
return render_template('Transactions.html')
Thanks @Giles。我已經通過你的教程,並嘗試使用SQLalchemy。想想幾乎在那裏,但在這裏卡住:'http:// stackoverflow.com/questions/42579079/flask-post-data-to-table-using-sqlalchemy-mysql' – wazzahenry