Browse Source

bottle and bottle-mysql-raw working

user 12 years ago
parent
commit
78822e4054
1 changed files with 13 additions and 12 deletions
  1. 13 12
      bottle/app.py

+ 13 - 12
bottle/app.py

@@ -1,21 +1,22 @@
 from bottle import Bottle, route, request, run
 from bottle.ext import sqlalchemy 
-from sqlalchemy import create_engine
+from sqlalchemy import create_engine, Column, Integer
 from sqlalchemy.ext.declarative import declarative_base
 from random import randint
+import json
 
 app = Bottle()
-app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://benchmarkdbuser:benchmarkdbpass@DBHOSTNAME:3306/hello_world'
+app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://benchmarkdbuser:benchmarkdbpass@192.168.0.12:3306/hello_world'
 Base = declarative_base()
 db_engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
 plugin = sqlalchemy.Plugin(db_engine, keyword='db', )
 app.install(plugin)
 
 
-class World(db.Model):
+class World(Base):
   __tablename__ = "World"
-  id = db.Column(db.Integer, primary_key=True)
-  randomNumber = db.Column(db.Integer)
+  id = Column(Integer, primary_key=True)
+  randomNumber = Column(Integer)
 
   # http://stackoverflow.com/questions/7102754/jsonify-a-sqlalchemy-result-set-in-flask
   @property
@@ -29,7 +30,7 @@ class World(db.Model):
 @app.route("/json")
 def hello():
   resp = {"message": "Hello, World!"}
-  return jsonify(resp)
+  return json.dumps(resp)
 
 @app.route("/db")
 def get_random_world(db):
@@ -37,14 +38,14 @@ def get_random_world(db):
   worlds = []
   for i in range(int(num_queries)):
     wid = randint(1, 10000)
-    worlds.append(World.query.get(wid).serialize)
-  return jsonify(worlds=worlds)
+    worlds.append(db.query(World).get(wid).serialize)
+  return json.dumps(worlds)
 
 @app.route("/dbs")
 def get_random_world_single(db):
   wid = randint(1, 10000)
-  worlds = [World.query.get(wid).serialize]
-  return jsonify(worlds=worlds)
+  worlds = [db.query(World).get(wid).serialize]
+  return json.dumps(worlds)
   
 @app.route("/dbraw")
 def get_random_world_raw():
@@ -56,7 +57,7 @@ def get_random_world_raw():
     result = connection.execute("SELECT * FROM world WHERE id = " + str(wid)).fetchone()
     worlds.append({'id': result[0], 'randomNumber': result[1]})
   connection.close()
-  return jsonify(worlds=worlds)
+  return jsondumps(worlds)
 
 @app.route("/dbsraw")
 def get_random_world_single_raw():
@@ -65,7 +66,7 @@ def get_random_world_single_raw():
   result = connection.execute("SELECT * FROM world WHERE id = " + str(wid)).fetchone()
   worlds = [{'id': result[0], 'randomNumber': result[1]}]
   connection.close()
-  return jsonify(worlds=worlds)
+  return json.dumps(worlds)
 
 if __name__ == "__main__":
     app.run()