Created
November 3, 2016 09:05
-
-
Save ioggstream/7a3e42eb8b9e0d4510f82e5c2788d951 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
import flask_sqlalchemy | |
from sqlalchemy.dialects.mysql import JSON | |
# A simple flask app connected to a db | |
app = flask.Flask('app') | |
app.config['SQLALCHEMY_DATABASE_URI']='mysql+mysqlconnector://root:secret@localhost:3306/test' | |
db = flask_sqlalchemy.SQLAlchemy(app) | |
# The model | |
class MyJson(db.Model): | |
name = db.Column(db.String(16), primary_key=True) | |
json = db.Column(JSON, nullable=True) | |
def __init__(self, name, json=None): | |
self.name = name | |
self.json = json | |
# Create table | |
db.create_all() | |
# Add an entry | |
entry = MyJson('jon', {'do': 'it', 'now': 1}) | |
db.session.add(entry) | |
db.session.commit() | |
# Get entry in Standard SQL | |
entries = db.engine.execute(db.select('*', from_obj=MyJson)).fetchall() | |
(name, json_as_string), = first_entry # unpack result (it's just one!) | |
assert isinstance(json_as_string, basestring) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment