2017-03-02 97 views
0

這是初學者試圖用燒瓶將數據插入到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') 

回答

1

您在SQL查詢中有錯誤

您的代碼:

c.execute("INSERT INTO transactions(Item, Shack, Paym_Reference, Amount) VALUES ('',%s,%s,%s,%s)") 

正確插入

c.execute("INSERT INTO transactions(Item, Shack, Paym_Reference, Amount) VALUES (%s,%s,%s,%s)") 

w3schools又如

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) 
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway'); 
2

PythonAnywhere開發商在這裏 - 你應該得到我們的系統相同的錯誤消息的任何其他 - 你檢查你的Web應用程序的錯誤日誌?

您的代碼有幾個問題。像Stanislav Filin說的那樣,你傳遞給c.execute的SQL中的空字符串不應該存在;你指定了四列,所以你只需要四個值。

但是你也需要傳入一些值來插入。 SQL字符串只是一個發送到數據庫的命令,您需要告訴系統用什麼值替換%s

所以不是

c.execute("INSERT INTO transactions(Item, Shack, Paym_Reference, Amount) VALUES ('',%s,%s,%s,%s)") 

...你應該有

c.execute("INSERT INTO transactions(Item, Shack, Paym_Reference, Amount) VALUES (%s,%s,%s,%s)", (Item, Shack, Paym_Reference, Amount)) 

更多的例子見this help page

最後一件事 - 在Flask應用程序中直接使用MySQL庫會非常困難。使用它有很多複雜性,特別是在連接管理方面。您上面給出的代碼將適用於您的網站的單個用戶,並且只要它的閒置時間不超過數據庫的連接超時(在PythonAnywhere上爲五分鐘),但隨後會因混淆錯誤消息而中斷。

SQLAlchemy是一個非常好的工具,您可以將它放在Flask代碼和處理連接管理的數據庫之間,並且可以很好地處理很多其他事情,並且可以節省您的工作量。一年前我寫了a blog post on building a database-backed website with Flask and SQLAlchemy,我真的建議你看看。

+0

Thanks @Giles。我已經通過你的教程,並嘗試使用SQLalchemy。想想幾乎在那裏,但在這裏卡住:'http:// stackoverflow.com/questions/42579079/flask-post-data-to-table-using-sqlalchemy-mysql' – wazzahenry