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

2024-04-20 08:50:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 数据方法脚本数据库表单sqlite网站方式
1条回答
网友
1楼 · 发布于 2024-04-20 08:50:12

下面是一个使用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。你知道吗

这应该足够让你开始了。你知道吗

相关问题 更多 >