python - 使用 Python 3/sqlite3 的 HTML 输出

标签 python html sqlite

Python 和 SQLLite 新手:@ http://www.sqlite.org/sqlite.html - “SQLite 的命令行 Shell” 我发现了这个:

更改输出格式: sqlite3 程序能够以八种不同的格式显示查询结果:“csv”、“column”、“html”、“insert”、“line”、“list”、“tabs”和“tcl”。您可以使用“.mode”点命令在这些输出格式之间切换。

问题:如何使用 Python 3 附带的捆绑 SQLite 包执行此操作 - 我可以打开连接、获取游标、执行 SQL 等,但我不知道如何使用 .mode html 命令 - 或任何点命令。

这是一个示例程序 - 但我想使用 sqlite .mode html 在最后一行以 html 格式输出结果“print(l)”

import sqlite3
def main():

    conn = sqlite3.connect('c:\dbTest.dat')
    curs=conn.cursor()
    fs='insert into test(token) values ("%s")'
    i=0
    x=input("Enter a number please: ")
    x.lstrip()
    x=int(x)
    while i<x:
        s=fs % ("ABCD"+str(i/0.999)) 
        curs.execute(s)
        i=i+1
    conn.commit()
    rows=curs.execute('select token from test')
    l=rows.fetchall()
    print(l)

main();

如有任何帮助,我们将不胜感激。

TIA

最佳答案

sqlite 的 html 模式是包装器的一部分,未包含在 python 中,但您可以使用 aspw访问它的模块...

import apsw
import StringIO as io
output=io.StringIO()
conn = apsw.Connection('dbTest.dat')
shell=apsw.Shell(stdout=output, db=conn)
# How to execute a dot command
shell.process_command(".mode html")
# continue

参见 the ASPW example了解更多详情

编辑

如果你使用的是python3...

Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import io
>>> import apsw
>>> output = io.StringIO()
>>> conn = apsw.Connection(":memory:")
>>> shell = apsw.Shell(stdout=output, db=conn)
>>> shell.process_command(".mode html")
>>> 

关于python - 使用 Python 3/sqlite3 的 HTML 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6092988/

相关文章:

html - 如何添加自定义图像背景以跨越 Bootstrap 按钮?

sqlite - 最小化 SQLite 库大小

python - 作为函数返回 : return()

python - 捕捉语法错误

javascript - 如何在 JQuery 中单击最近的元素(复选框)时禁用下一个元素(文本区域)?

php - 我创建了一个 CSS 切换器,但我只需要更改 body 类为 ="red"的元素

java - 每次我打开新 Activity 时,Android SQLite 数据库似乎都会被清除

c# - MySql 和 SQlite 类实现接口(interface)

python - 如何使用 to_dict() 创建一个键、值对均为整数的字典?

python - 如果该子列表的任何元素在另一个列表中,如何删除列表(即子列表)中的列表?