0

I am trying to connect to a database object I created for use in a web app.

The python code for the app is the following:

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)

I used the code in "class bars_data()" to originally create the data base file which is in the same directory as the rest of the source code files but since I do not run it with the app I commented it out. I am not sure if I need to recreate the database/table when I run the app so included it if this is the case. I want to access the bar names from the database and write it to the page using jinja.

I am using the Google App Engine to run it. I know I could use the data store but this seemed an easier way to create a static database.

When I try to run the app nothing will render. But when I remove the code tyring to make the database connection my "index.html" template will again render in the browser. What am I doing wrong?

tuomur
  • 6,888
  • 34
  • 37
user2907249
  • 839
  • 7
  • 14
  • 32
  • Can you obtain traceback somehow? Enable debugging mode or wrap the code into try-catch and render traceback. I have no experience with GAE, but I would say you shouldn't store data to files (including sqlite3) given the distributed nature of the platform... See here: http://stackoverflow.com/a/2693092/196206 But maybe something changed since then. – Messa May 07 '15 at 06:49
  • 1
    This can't possibly work. GAE apps can not write to the filesystem. – Daniel Roseman May 07 '15 at 07:28

1 Answers1

0

If you are providing the database file as a part of the application files this should work. Just make sure the file path is correct as mentioned in this answer. Do not assume the application's working directory and the data directory are the same.

Community
  • 1
  • 1
tuomur
  • 6,888
  • 34
  • 37