[windows] flask - sqlite3.OperationalError: unable to open database file

# all the imports
##from __future__ import with_statement
import sqlite3

from flask import Flask, request, session, g, redirect,url_for, \
     abort, render_template, flash


from contextlib import closing

import os

# configuration
DATABASE = 'flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = '123'



# create our little application
app = Flask(__name__)
app.config.from_object(__name__)

for k, v in app.config.items():
    print 'config[%s] = %s' % (k, v) 
    
def connect_db():
    return sqlite3.connect(app.config.get('DATABASE'))

def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql') as f:
            db.cursor().executescript(f.read())
        db.commit()

if __name__ == '__main__':
    app.run()


from flaskr import init_db
init_db()

出现错误:sqlite3.OperationalError: unable to open database file

解决:

我的项目路径在E:\_PythonCode\flaskr下

在Python shell 上查看当前路径:

import os
os.getcwd()
》'E:\\_PythonCode'

切换路径到项目路径下:

os.chdir('./flaskr')

此时路径为E:\_PythonCode\flaskr

from flaskr import init_db
init_db()

此时E:\_PythonCode\flaskr下会自动创建  flaskr.db 文件




你可能感兴趣的:(python,web)