[英]Flask OperationalError: unable to open database file using sqlite3
I'm trying to hook an existing sqlite3 db to a dashboard I'm building and I've run into a problem that I can't figure out how to resolve. 我正在尝试将现有的sqlite3数据库挂钩到我正在构建的仪表板上,我遇到了一个我无法弄清楚如何解决的问题。 I've been working on this by trying to piece together things from the Flask docs and other sources, so feel free to call me out on anything in here that looks a little odd. 我一直在努力将Flask文档和其他来源的内容拼凑起来,所以请随时给我打电话,看看有点奇怪的事情。 It probably is, and I just don't know it :) 它可能是,我只是不知道:)
Code: 码:
from __future__ import with_statement
from contextlib import closing
from flask import Flask, render_template, request, session, g, redirect, url_for, abort, flash
import sqlite3
#config
DATABASE = '~/home/aaron/Dropbox/coding/webapp2/tmp/test.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
app = Flask(__name__)
app.config.from_object(__name__)
def connect_db():
return sqlite3.connect(app.config['DATABASE']) # LINE 17
@app.before_request
def before_request():
g.db = connect_db() # LINE 22
@app.teardown_request
def teardown_request(exception):
if hasattr(g, 'db'):
g.db.close()
# App seems to error out before app.route and if __name__=='__main__' block
# Everything in my app.route is commented out
Complete error: 完成错误:
Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in call return self.wsgi_app(environ, start_response) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1356, in full_dispatch_request rv = self.preprocess_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1539, in preprocess_request rv = func() File "/home/aaron/Dropbox/coding/webapp2/control.py", line 22, in before_request g.db = connect_db() File "/home/aaron/Dropbox/coding/webapp2/control.py", line 17, in connect_db return sqlite3.connect(app.config['DA 回溯(最近一次调用最后一次):文件“/usr/local/lib/python2.7/dist-packages/flask/app.py”,第1701行,在调用返回self.wsgi_app(environ,start_response)文件“/ usr /local/lib/python2.7/dist-packages/flask/app.py“,第1689行,在wsgi_app response = self.make_response(self.handle_exception(e))文件”/usr/local/lib/python2.7 /dist-packages/flask/app.py“,第1687行,在wsgi_app response = self.full_dispatch_request()文件”/usr/local/lib/python2.7/dist-packages/flask/app.py“,第1360行,在full_dispatch_request中rv = self.handle_user_exception(e)文件“/usr/local/lib/python2.7/dist-packages/flask/app.py”,第1356行,在full_dispatch_request中rv = self.preprocess_request()文件“/ usr / local / lib / python2.7 / dist-packages / flask / app.py“,第1539行,在preprocess_request中rv = func()文件”/home/aaron/Dropbox/coding/webapp2/control.py“,行22,在before_request中g.db = connect_db()文件“/home/aaron/Dropbox/coding/webapp2/control.py”,第17行,在connect_db中返回sqlite3.connect(app.config ['DA TABASE']) OperationalError: unable to open database file TABASE'])OperationalError:无法打开数据库文件
127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /? debugger =yes&cmd=resource&f=style.css HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /? debugger =yes&cmd=resource&f=jquery.js HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /? debugger =yes&cmd=resource&f=debugger.js HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /? debugger =yes&cmd=resource&f=console.png HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:48] "GET /? debugger =yes&cmd=resource&f=source.png HTTP/1.1" 200 - 127.0.0.1 - - [13/Oct/2012 13:55:49] "GET /favicon.ico HTTP/1.1" 500 - 127.0.0.1 - - [13 / Oct / 2012 13:55:48]“GET /? debugger = yes&cmd = resource&f = style.css HTTP / 1.1”200 - 127.0.0.1 - - [13 / Oct / 2012 13:55 :48]“GET /? debugger = yes&cmd = resource&f = jquery.js HTTP / 1.1”200 - 127.0.0.1 - - [13 / Oct / 2012 13:55:48]“GET /? debugger = yes&cmd = resource&f = debugger .js HTTP / 1.1“200 - 127.0.0.1 - - [13 / Oct / 2012 13:55:48]”GET /? debugger = yes&cmd = resource&f = console.png HTTP / 1.1“200 - 127.0.0.1 - - [ 13 / Oct / 2012 13:55:48]“GET /? debugger = yes&cmd = resource&f = source.png HTTP / 1.1”200 - 127.0.0.1 - - [13 / Oct / 2012 13:55:49]“GET / favicon.ico HTTP / 1.1“500 -
Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1701, in call return self.wsgi_app(environ, start_response) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app response = self.full_dispatch_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1356, in full_dispatch_request rv = self.preprocess_request() File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1539, in preprocess_request rv = func() File "/home/aaron/Dropbox/coding/webapp2/control.py", line 22, in before_request g.db = connect_db() File "/home/aaron/Dropbox/coding/webapp2/control.py", line 17, in connect_db return sqlite3.connect(app.config['DA 回溯(最近一次调用最后一次):文件“/usr/local/lib/python2.7/dist-packages/flask/app.py”,第1701行,在调用返回self.wsgi_app(environ,start_response)文件“/ usr /local/lib/python2.7/dist-packages/flask/app.py“,第1689行,在wsgi_app response = self.make_response(self.handle_exception(e))文件”/usr/local/lib/python2.7 /dist-packages/flask/app.py“,第1687行,在wsgi_app response = self.full_dispatch_request()文件”/usr/local/lib/python2.7/dist-packages/flask/app.py“,第1360行,在full_dispatch_request中rv = self.handle_user_exception(e)文件“/usr/local/lib/python2.7/dist-packages/flask/app.py”,第1356行,在full_dispatch_request中rv = self.preprocess_request()文件“/ usr / local / lib / python2.7 / dist-packages / flask / app.py“,第1539行,在preprocess_request中rv = func()文件”/home/aaron/Dropbox/coding/webapp2/control.py“,行22,在before_request中g.db = connect_db()文件“/home/aaron/Dropbox/coding/webapp2/control.py”,第17行,在connect_db中返回sqlite3.connect(app.config ['DA TABASE']) OperationalError: unable to open database file TABASE'])OperationalError:无法打开数据库文件
It seems that the problem is coming from this config line: 似乎问题来自这个配置行:
DATABASE = '~/home/aaron/Dropbox/coding/webapp2/tmp/test.db'
My questions: 我的问题:
1) Why is the OperationalError being thrown twice? 1)为什么要抛出两次OperationalError?
2) Why does each OperationalError call out lines 17 and 22 (commented in my code above), even though these are function definitions and and not function calls? 2)为什么每个OperationalError调用第17行和第22行(在我上面的代码中注释),即使这些是函数定义而不是函数调用?
3) How do I resolve the error, given that this is a valid db with data at the path specified? 3)如果这是一个在指定路径上有数据的有效数据库,我如何解决错误?
These are what I'm referencing: 这些是我所引用的:
http://flask.pocoo.org/docs/tutorial/dbcon/#tutorial-dbcon http://flask.pocoo.org/docs/tutorial/dbcon/#tutorial-dbcon
http://flask.pocoo.org/docs/tutorial/views/#tutorial-views http://flask.pocoo.org/docs/tutorial/views/#tutorial-views
http://flask.pocoo.org/docs/patterns/sqlite3/ http://flask.pocoo.org/docs/patterns/sqlite3/
I think that the problem is the ~
character (valid in shell but not in Python), so you will probably need to write the full absolute path. 我认为问题是~
字符(在shell中有效但在Python中没有),因此您可能需要编写完整的绝对路径。 I am not using Flask but I suggest to set up PROJECT_ROOT constant in your settings and then use relative paths: 我没有使用Flask,但我建议在你的设置中设置PROJECT_ROOT常量,然后使用相对路径:
import os
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
DATABASE = os.path.join(PROJECT_ROOT, 'tmp', 'test.db')
This Worked for me : 这对我有用:
When you define database in the beginning, don't just say app.database = 'example.db'
, instead, you have to specify the home directory path with double slashes : 在开头定义数据库时,不要只说app.database = 'example.db'
,而是必须使用双斜杠指定主目录路径:
"ex : //var//www//foldername//example.db"
if you're using linux and restart the server again. "ex : //var//www//foldername//example.db"
如果你正在使用linux并重新启动服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.