如何解决使用Python 3 / sqlite3的HTML输出
|| Python和SQLLite的新手:@ http://www.sqlite.org/sqlite.html-“用于SQLite的命令行外壳”我发现了这一点: 更改输出格式: 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模式是per1ѭ中未包含的包装器的一部分,但是您可以使用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
有关更多详细信息,请参见ASPW示例
编辑
如果您使用的是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\")
>>>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。