Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从sqlite表创建html表_Python_Html_Sqlite_Python 2.7 - Fatal编程技术网

Python 从sqlite表创建html表

Python 从sqlite表创建html表,python,html,sqlite,python-2.7,Python,Html,Sqlite,Python 2.7,我有一个名为“seinfeldFood.sqlite”的数据库,它有4个表:“剧集”、“食品”、“食品剧集”和“食品类型”。我想编写一个程序,以便用户可以从“seinfeldFood.sqlite”中选择一个表,并将表的内容显示在HTML文件中。此外,HTML表应该1)有一个与表名相同的标题,2)应该有一个标题作为表的列名 我编写了以下代码,试图将sqlite表的内容添加到一个HTML文件中,但得到了一个空白的HTML文件 import webbrowser import os import s

我有一个名为“seinfeldFood.sqlite”的数据库,它有4个表:“剧集”、“食品”、“食品剧集”和“食品类型”。我想编写一个程序,以便用户可以从“seinfeldFood.sqlite”中选择一个表,并将表的内容显示在HTML文件中。此外,HTML表应该1)有一个与表名相同的标题,2)应该有一个标题作为表的列名

我编写了以下代码,试图将sqlite表的内容添加到一个HTML文件中,但得到了一个空白的HTML文件

import webbrowser
import os
import sqlite3 as sqlite

def getTable(cursor):
    cursor.execute("""SELECT tbl_name FROM sqlite_master WHERE type = 'table'""")
    tables = [t[0] for t in cursor.fetchall()]
    print tables
    prompt = "Select table name from: %s\n"%" ".join(tables)
    while True:
        table = raw_input(prompt)
        if table in tables:
            return table

def createTable(cursor,tableName):
    category = []
    results = cursor.execute(("""SELECT * FROM %s""")%(tableName))
    for r in results:
        category.append(r[1])
    cursor.execute("CREATE TABLE IF NOT EXISTS Category (name)")
    cursor.execute("""INSERT INTO Category (name) VALUES (?)""",("Category",))
    for c in category:
        cursor.execute("""INSERT INTO Category (name) VALUES (?)""", (c,))
    #conn.commit()
    #result_new = cursor.execute("""SELECT * FROM Category""")
    #for t in result_new:
        #return t

def getHTML(cursor,tableName):
    html = \
        """<html>
        <head>
        <title>%s</title>
        <script>
        function createTable(%s,%s)
        {
        }
        </script>
        </head>
        </html>
        """%(tableName,cursor,tableName)
    return html    

def viewDatabase(dbname):
    fo = open(os.path.join(dataDir,"tableView.html"),"w")
    fo.write( getHTML(cursor,tablename,))
    fo.close()
    webbrowser.open_new(dbname)
以下是该表的图像:


您似乎对许多核心概念感到困惑。 1) 如果您已经有一个sqlite表:您不需要“如果不存在则创建表” 2) javascript来填充HTML表

纯HTML中的表代码将包含如下内容:

html_part='<table>'
for t in result_new:
    html_part=html_part + '<tr><td>' + t + '</td></tr>'
html_part=html_part + '</table>
html\u部分=“”
对于结果中的t_new:
html\u part=html\u part+“”+t+“”
html\u part=html\u part+'
然后在生成的html文档中的正确位置插入html_部分

使用webframework可能会更好。试一试


然后,您可以使用smartgrid创建一个可编辑和可搜索的数据库表。

除了空白HTML文件之外,您还需要什么?你从来没有在那个文件中放过任何实际内容,只是一个空的JS函数。如何修改它?thanks@neymar您可以通过在其中放入一个实际的表来修改它(可能不是一个空的
createTable
JS函数)。您的实际问题是“如何使用Python创建HTML表?”我只是不知道如何将表放入HTML。我想将数据库表(sqlite)的内容复制到HTML以成为HTML表。您是否尝试过我的代码?->您可以将其粘贴到您的代码中,您可以在其中提到:#result_new
html_part='<table>'
for t in result_new:
    html_part=html_part + '<tr><td>' + t + '</td></tr>'
html_part=html_part + '</table>