Python 连接到web应用程序中的SQLite数据库

Python 连接到web应用程序中的SQLite数据库,python,google-app-engine,sqlite,Python,Google App Engine,Sqlite,我正在尝试连接到我创建的用于web应用程序的数据库对象 应用程序的python代码如下所示: import os import jinja2 import webapp2 import sqlite3 #from google.appengine.ext import db template_dir = os.path.join(os.path.dirname(__file__), 'templates') jinja_env = jinja2.Environment(loader = jin

我正在尝试连接到我创建的用于web应用程序的数据库对象

应用程序的python代码如下所示:

import os
import jinja2
import webapp2
import sqlite3
#from google.appengine.ext import db

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a,**kw)

    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)

    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

#class bars_data():
#    def createdb(self):
        #barsdb = sqlite3.connect('bardatabase.db')
        #barsdb.execute("DROP TABLE if exists bars")
        #barsdb.execute("create table bars(name text, address text, state int)")
        #barsdb.execute('Insert into bars (name, address, state) VALUES(?,?,?)',("Hoolies", '24 Glebe Rd', 10))
        #barsdb.execute('Insert into bars (name, address, state) VALUES(?,?,?)',("Bar1", '25 Main St', 10))
        #barsdb.execute('Insert into bars (name, address, state) VALUES(?,?,?)',('Cafe Doug', '35 Summer St.', 5))
        #barsdb.execute('Insert into bars (name, address, state) VALUES(?,?,?)',("McMurphy", '25 F Street', 20))
        #barsdb.commit()       

class MainPage(Handler):
    def get(self):
        barsdb = sqlite3.connect('bardatabase.db')
        query = barsdb.cursor()
        bars_list = barsdb.execute("SELECT name FROM barsdb")

        self.render("index.html")

app = webapp2.WSGIApplication([
    ('/', MainPage)
], debug=True)
我使用“class bars_data()”中的代码最初创建了数据库文件,该文件与其他源代码文件位于同一目录中,但由于我没有在应用程序中运行它,因此我将其注释掉。我不确定在运行应用程序时是否需要重新创建数据库/表,因此如果是这种情况,请将其包括在内。我想从数据库中访问酒吧名称,并使用jinja将其写入页面

我正在使用谷歌应用程序引擎来运行它。我知道我可以使用数据存储,但这似乎是创建静态数据库的一种更简单的方法


当我尝试运行应用程序时,将不会呈现任何内容。但是,当我删除使数据库连接的代码时,我的“index.html”模板将再次在浏览器中呈现。我做错了什么?

如果您将数据库文件作为应用程序文件的一部分提供,这应该可以工作。只需确保文件路径正确,如中所述。不要假设应用程序的工作目录和数据目录相同。

您能以某种方式获得回溯吗?启用调试模式或将代码包装到try-catch和render-traceback中。我没有使用GAE的经验,但我想说,鉴于平台的分布式特性,您不应该将数据存储到文件(包括sqlite3)中。。。看这里:但也许从那以后发生了一些变化。这不可能奏效。GAE应用程序无法写入文件系统。