flask下,实现简单博客2
2013-02-20 11:32
281 查看
flask下,实现简单博客2
创建数据库
将上面的代码保存到schema.sql 文件中。
创建views.py
from __future__ import with_statement
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, _app_ctx_stack
# configuration
DATABASE = 'blog.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = '123'
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def init_db():
"""Creates the database tables."""
with app.app_context():
db = get_db()
with app.open_resource('schema.sql') as f:
db.cursor().executescript(f.read())
db.commit()
在shell模式下运行
执行完后,将创建数据库和表。
创建数据库
drop table if exists entries; create table entries ( id integer primary key autoincrement, title string not null, text string not null );
将上面的代码保存到schema.sql 文件中。
创建views.py
from __future__ import with_statement
from sqlite3 import dbapi2 as sqlite3
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, _app_ctx_stack
# configuration
DATABASE = 'blog.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = '123'
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
def init_db():
"""Creates the database tables."""
with app.app_context():
db = get_db()
with app.open_resource('schema.sql') as f:
db.cursor().executescript(f.read())
db.commit()
在shell模式下运行
from flaskr import init_db >>> init_db()
执行完后,将创建数据库和表。
相关文章推荐
- python+flask实现简单博客
- python基础学习——利用Flask实现一个简单博客
- flask下,实现简单博客3
- flask下,实现简单博客4
- 基于flask实现的简单博客
- flask下,实现简单博客5
- 第一篇博客 简单调研了一下百度语音 以及简单实现一些根据语音获取网络连接
- Android中的微信、支付宝支付功能的简单实现--摘自肖玲玲老师博客160305
- 用 Flask 来写个轻博客 (22) — 实现博客文章的添加和编辑页面
- 浏览器九宫格的简单实现 - 蒋宇捷的专栏 - 博客频道 - CSDN.NET
- 使用Flask+MongoDB实现基于REST的接口简单操作
- 使用Python Flask框架快速实现简单API
- flask + mysql + highcharts(动态刷新)实现的简单监控系统
- php实现的简单的csdn博客文章抓取
- 简单CSDN爬虫,实现博客访问量记录
- nodejs+express+mongodb简单实现注册登录写发博客
- 用 Flask 来写个轻博客 (27) — 使用 Flask-Cache 实现网页缓存加速
- 使用 flask 实现一个简单的可以返回 json 的 RESTful API 服务端
- WebService系列博客{十}[CXF简单案例实现]
- 基于CakePHP实现的简单博客系统实例