【问题标题】:How to return value web from sqlite?如何从sqlite返回价值网络?
【发布时间】:2015-04-07 12:45:11
【问题描述】:

这是我的代码。我想在网上展示这些价值观。怎么办?

import sqlite3

def application(environ, start_response):

 db = sqlite3.connect('/root/example.db')
 db.row_factory = sqlite3.Row
 cursor = db.cursor()
 cursor.execute('''SELECT id, message,date FROM table''')
 for row in cursor:
  print('{0} : {1}, {2}'.format(row['id'], row['message'], row['date']))
 db.close()

 start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])

【问题讨论】:

标签: python sqlite wsgi


【解决方案1】:

构建要返回的字符串列表;打印写入stdout 并且不会返回给浏览器。

db = sqlite3.connect('/root/example.db')
db.row_factory = sqlite3.Row
cursor = db.cursor()
cursor.execute('''SELECT id, message,date FROM table''')

results = []
for row in cursor:
    results.append('{0} : {1}, {2}'.format(row['id'], row['message'], row['date']))
db.close()


headers = [
    ('Content-Type', 'text/html; charset=utf-8'),
    ('Content-Length', str(sum(len(line) for line in results)))
]

start_response('200 OK', headers)

return results

【讨论】:

  • 谢谢马丁。有用。你知道如何做同样的事情,但在表格中显示结果 html 值
  • @marcosbarcelona:在输出中包含表格的 HTML; results = ['<table>'],然后为每一行添加您的 <tr><td> 标记,并将最后一个 </table> 字符串附加到列表中。
猜你喜欢
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
相关资源
最近更新 更多