2017-03-02 126 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