2017-05-09 15 views
-1

我需要一些帮助来找出为什么我的结果只打印出表格中最后一行数据。用Python将Microsoft SQL数据打印到网页上

from flask import Flask, render_template, redirect, request 

import pyodbc 

#server = 'EVERETT-PC\SQLEXPRESS' 
#db = 'AdventureWorks2008R2' 

con = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL Server}',server = 'EVERETT-PC\SQLEXPRESS' , database = 'iNcentDev') 

cur = con.cursor() 
cur.execute("SELECT * FROM app.Currency") 
s = "<table style= 'border:1px solid red'>" 
for rows in cur: 
    s = s + "<tr>" 
for x in rows: 
    s = s + "<td>" + str(x) + "</td>" 
s = s + "</tr>" 

con.close 

app=Flask(__name__) 
@app.route('/') 
@app.route('/home') 
def home(): 
    return "<html><body>" + s + "</body></html>" 

if __name__=="__main__": 
    app.run(debug=True) 

The data I would like to print.

my results

回答

0

您需要使用fetchall方法。

rows = cur.fetchall() 

for row in rows: 
    #....