在一天的大部分时间里,我一直在为此苦苦挣扎。我绝对无法在 PyCharm 中连接到我的数据库。我什至在从 github 下载教程源时完全按照视频教程进行操作。请帮忙!
http://www.youtube.com/watch?v=jaFEODzG7B8 https://github.com/mitsuhiko/flask/tree/master/examples/flaskr/
# all the imports
from __future__ import with_statement
from contextlib import closing
import sqlite3
from flask import Flask, request, session, g, redirect, url_for,\
abort, render_template, flash
# configuration
DATABASE = "flaskr.db"
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
def connect_db():
return sqlite3.connect(app.config['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()
尝试初始化数据库时出现以下错误:
>>> init_db()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\Intern2\PycharmProjects\flaskr\flaskr.py", line 23, in init_db
with closing(connect_db()) as db:
File "C:\Users\Intern2\PycharmProjects\flaskr\flaskr.py", line 20, in connect_db
return sqlite3.connect(app.config['DATABASE'])
OperationalError: unable to open database file
从字面上看,任何理论都将不胜感激!
更新:我创建了数据库文件,但现在我无法找到它。我尝试了多个版本的路径。
DATABASE = '/Users/snuffles753/PycharmProjects/flaskr/flaskr.db'
DATABASE = 'flaskr.db
等等...该文件与我的 .py 文件位于完全相同的位置
另外,我正在使用 Mac OS