From fed3603ab6ebf1a81d0cae548ace3cc8286aa163 Mon Sep 17 00:00:00 2001 From: Thibaud Morel Date: Mon, 12 Mar 2012 11:51:46 -0700 Subject: [PATCH 1/3] Adding DB creation to the example --- examples/hello.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/hello.py b/examples/hello.py index 9235559e..24dfa0c7 100644 --- a/examples/hello.py +++ b/examples/hello.py @@ -57,4 +57,5 @@ def update_done(): if __name__ == '__main__': + db.create_all() app.run() From d7d8ba832f56b8a108faa56c9f951bee43a81ce2 Mon Sep 17 00:00:00 2001 From: Thibaud Morel Date: Mon, 12 Mar 2012 12:33:09 -0700 Subject: [PATCH 2/3] Added basic quickstart guide --- README | 3 --- README.rst | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) delete mode 100644 README create mode 100644 README.rst diff --git a/README b/README deleted file mode 100644 index 5120fe91..00000000 --- a/README +++ /dev/null @@ -1,3 +0,0 @@ -Flask-SQLAlchemy - -Adds SQLAlchemy support to Flask. Under development. diff --git a/README.rst b/README.rst new file mode 100644 index 00000000..c8936def --- /dev/null +++ b/README.rst @@ -0,0 +1,66 @@ +================ +Flask-SQLAlchemy +================ + +Adds SQLAlchemy support to Flask. Under development. + +Quickstart +========== + +Install the package:: + + pip install Flask-SQLAlchemy + +In your Flask app, import the extension and initialize the DB:: + + from flaskext.sqlalchemy import SQLAlchemy + app.config['SQLALCHEMY_ENGINE'] = 'sqlite:////code/apicurious/db.sqlite' + app.config['SQLALCHEMY_ECHO'] = True + db = SQLAlchemy(app) + +Instead of declaring your models via ``declarative_base()`` as you usually +would with SQLAlchemy, subclass from db.Model:: + + class Note(db.Model): + __tablename__ = "notes" + + id = db.Column(db.Integer, primary_key=true) + text = db.Column(db.String(length=255)) + + def __init__(self, text): + self.text = text + +To create the database tables, run:: + + db.create_all() + +If you've set ``app.config['SQLALCHEMY_ECHO'] = True``, you will see the SQL +statments that the database will execute:: + + 2012-03-12 12:15:07,793 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("notes") + 2012-03-12 12:15:07,793 INFO sqlalchemy.engine.base.Engine () + 2012-03-12 12:15:07,794 INFO sqlalchemy.engine.base.Engine + CREATE TABLE notes ( + id INTEGER NOT NULL, + text VARCHAR(255), + PRIMARY KEY (id) + ) + + + 2012-03-12 12:15:07,794 INFO sqlalchemy.engine.base.Engine () + 2012-03-12 12:15:07,794 INFO sqlalchemy.engine.base.Engine COMMIT + +To create a record, use ``db.session.add``. For more information on sessions, +refer to the `SQLAchemy documention on sessions +`_:: + + note = Note('Hello World') + db.session.add(note) + db.session.commit() + +To query, use ``.query``:: + + >>> print Note.query.get(1).text + 'Hello World' + + From 89563fcad3c7a77d71b46ab8aa8834edc6ee0821 Mon Sep 17 00:00:00 2001 From: Thibaud Morel Date: Mon, 12 Mar 2012 14:17:57 -0700 Subject: [PATCH 3/3] fixed database configuration issue in README --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index c8936def..e5018a6e 100644 --- a/README.rst +++ b/README.rst @@ -14,7 +14,7 @@ Install the package:: In your Flask app, import the extension and initialize the DB:: from flaskext.sqlalchemy import SQLAlchemy - app.config['SQLALCHEMY_ENGINE'] = 'sqlite:////code/apicurious/db.sqlite' + app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////code/apicurious/db.sqlite' app.config['SQLALCHEMY_ECHO'] = True db = SQLAlchemy(app)