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?