【问题标题】:using sqlite3 database for flaskr in python without ORM在没有ORM的python中为flaskr使用sqlite3数据库
【发布时间】:2016-01-19 12:34:51
【问题描述】:

我正在尝试将 sqlite3 用于我的 python 项目。我需要制作一个注册页面来注册用户以访问其他功能。所以我需要检查用户名或电子邮件是否存在。我被禁止使用 ORM(对不起我的英语不好)。我通过的唯一教程是这个:http://flask.pocoo.org/docs/0.10/tutorial/introduction/ 但我不知道如何使用数据库,如何获取表格信息和单元格以及...... 你能告诉我怎么做或给我一个参考吗? 我想我必须知道一些这样的命令:

@app.route('/')
def show_entries():
    #specially this line
    cur = g.db.execute('select title, text from entries order by id desc')
    entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
    return render_template('show_entries.html', entries=entries)

【问题讨论】:

  • 看看教程中的source code。它应该有帮助!
  • 我做了这个小应用程序,它运行良好。我只想使用我的 python 项目中的 sqlite3 数据库。
  • 你需要了解一些SQL;知道如何使用 python 和 sqlite3(built-in module: sqlite3),如何使用 sqlite3 和 flask(Using SQLite 3 with Flask)。

标签: python flask sqlite


【解决方案1】:

基本上是这样的:

t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print c.fetchone()

更全面的例子可以在这里找到:https://docs.python.org/2/library/sqlite3.html

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 2016-02-21
    • 2014-10-10
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多