Python从我的数据库(sqlite)显示网站上的数据

问题描述 投票:-1回答:1

我有几个python脚本可以对我的sqlite数据库执行一些操作并将其打印到控制台。将其放在网站上的最佳方式是什么,而不是将其打印到控制台,数据显示在网站上。我需要从网站上的表单中获取一些输入,并在脚本中调用特定的python方法来执行它需要的东西(取决于表单)并在浏览器中显示结果。 谢谢

python database forms python-3.x sqlite
1个回答
0
投票

这是一个使用bottle.py的简单骨架

from bottle import run, get, post, request

@get("/")
def index():
    return '''
            <form action="/sqlite/data" method="post">
                Input 1: <input name="input1" type="text" />
                Input 2: <input name="input2" type="text" />
                <input value="Submit" type="submit" />
            </form>
        '''
@post('/sqlite/data')
def open():
    input1 = request.forms.get('input1')
    input2 = request.forms.get('input2')
    print("I received", input1, "and", input2)
    # do your sqlite operation here, return the result in the browser
    result = "hello from sqlite"
    return result

run(host='localhost', port=8080, debug=True, reloader=True)

安装瓶然后运行代码,在http://localhost:8080打开浏览器

因此,一旦提交了表单,就会显示一个简单的表单,它将由open函数接收,其中sqlite逻辑应该存在,处理后将响应返回给浏览器html或json。

这应该足以让你开始。

© www.soinside.com 2019 - 2024. All rights reserved.