Flask SQLite 查询数据库
Flask SQLite querying database
我正在尝试对我的数据库执行一些操作。 table 'orders' 在创建函数中获取。但为什么它在 showtable() 函数中不起作用?这似乎是一个逻辑错误。我也想使用聚合函数。我怎样才能 return query1() 的结果?
from flask import Flask, render_template, request, url_for, redirect
import sqlite3 as sql
app = Flask(__name__)
def dbconn():
con = sql.connect("onlineshop.db")
con.row_factory = sql.Row
cur = con.cursor()
return cur
@app.route('/createdb',methods = ['POST', 'GET'])
def create():
cur=dbconn()
cur.execute("CREATE TABLE orders(item TEXT, price REAL, cust_name TEXT)")
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Oximeter","50","Alice"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Sanitizer","20","Paul"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Mask","10","Anita"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Sanitizer","20","Tara"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Thermometer","30","Bob"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Mask","10","Alice"))
#return " Table is created."
cur.execute("select * from orders")
rows = cur.fetchall()
return render_template("onlineshoprecords.html", rows = rows)
@app.route('/showtable',methods = ['GET','POST'])
def showtable():
cur=dbconn()
cur.execute("SELECT * FROM orders")
rows = cur.fetchall()
return render_template("onlineshoprecords.html", rows = rows)
@app.route("/query1",methods = ['GET','POST'])
def query1():
cur=dbconn()
res1=cur.execute("select sum(price) from orders")
return f' result is :{res1}'
if __name__ == '__main__':
app.run(debug = True)
HTML
<!DOCTYPE html>
<head></head>
<body>
<table>
<tr>
<th>Item</th>
<th>Price</th>
<th>Customer Name</th>
</tr>
{% for row in rows %}
<tr align="center">
<td>{{row["item"]}} </td>
<td>{{row["price"]}}</td>
<td>{{row["cust_name"]}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
之后
cur=dbconn()
cur.execute("SELECT * FROM orders")
rows = cur.fetchall()
rows
是 tuple
的 list
,而不是 dict
,因此在您的模板中,您应该通过提供索引而不是键来访问它,即替换
<td>{{row["item"]}} </td>
<td>{{row["price"]}}</td>
<td>{{row["cust_name"]}}</td>
使用
<td>{{row[0]}}</td>
<td>{{row[1]}}</td>
<td>{{row[2]}}</td>
我正在尝试对我的数据库执行一些操作。 table 'orders' 在创建函数中获取。但为什么它在 showtable() 函数中不起作用?这似乎是一个逻辑错误。我也想使用聚合函数。我怎样才能 return query1() 的结果?
from flask import Flask, render_template, request, url_for, redirect
import sqlite3 as sql
app = Flask(__name__)
def dbconn():
con = sql.connect("onlineshop.db")
con.row_factory = sql.Row
cur = con.cursor()
return cur
@app.route('/createdb',methods = ['POST', 'GET'])
def create():
cur=dbconn()
cur.execute("CREATE TABLE orders(item TEXT, price REAL, cust_name TEXT)")
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Oximeter","50","Alice"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Sanitizer","20","Paul"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Mask","10","Anita"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Sanitizer","20","Tara"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Thermometer","30","Bob"))
cur.execute("INSERT into orders (item, price, cust_name) values (?,?,?)",("Mask","10","Alice"))
#return " Table is created."
cur.execute("select * from orders")
rows = cur.fetchall()
return render_template("onlineshoprecords.html", rows = rows)
@app.route('/showtable',methods = ['GET','POST'])
def showtable():
cur=dbconn()
cur.execute("SELECT * FROM orders")
rows = cur.fetchall()
return render_template("onlineshoprecords.html", rows = rows)
@app.route("/query1",methods = ['GET','POST'])
def query1():
cur=dbconn()
res1=cur.execute("select sum(price) from orders")
return f' result is :{res1}'
if __name__ == '__main__':
app.run(debug = True)
HTML
<!DOCTYPE html>
<head></head>
<body>
<table>
<tr>
<th>Item</th>
<th>Price</th>
<th>Customer Name</th>
</tr>
{% for row in rows %}
<tr align="center">
<td>{{row["item"]}} </td>
<td>{{row["price"]}}</td>
<td>{{row["cust_name"]}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
之后
cur=dbconn()
cur.execute("SELECT * FROM orders")
rows = cur.fetchall()
rows
是 tuple
的 list
,而不是 dict
,因此在您的模板中,您应该通过提供索引而不是键来访问它,即替换
<td>{{row["item"]}} </td>
<td>{{row["price"]}}</td>
<td>{{row["cust_name"]}}</td>
使用
<td>{{row[0]}}</td>
<td>{{row[1]}}</td>
<td>{{row[2]}}</td>