flask 怎么调用 sqlite,第1张

flask 怎么调用 sqlite,第2张

需要将 db 里的数据输出到网页,不知道为什么一直不对

下面是部分源码

books = get_db()
cur = books.execute('select * from BOOKS')
id = []
for row in cur:
id.append(row[0])
ID = id
return render_template("index.html",ID=ID)

函数

DATABASE = 'books.db'
def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql', mode='r') as f:
            db.cursor().executescript(f.read())
        db.commit()

def connect_db():
    return sqlite3.connect(DATABASE)
def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = connect_db()
    return db

模板

{% for i in ID %}
    <div>{{i}}</div>
    {% endfor %}

确定 books.db 里有数据,但结果什么都不显示

----------------------- 以下是精选回复-----------------------

答:DATABASE 用绝对路径试试?
答:flask 调用 sqlite 的方式和 python  调用 sqlite 的方式一样

你可以先不用渲染模板 直接在 shell 里面试一试

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » flask 怎么调用 sqlite

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情