Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
SQLAlchemy - first examples with a Python Object Relationship Mapping system

Object to Relational Database mapping - in other words how to you take objects in your language such as Python and save them in database tables, and vice versa, without having to do all the detailed SQL coding yourself? Use on ORM system. An example in Python is SQLalchemy which connects to a variety of databases - and we have a couple of "demos" of it in action [here] and [here].

Here's sample output from the second of those - not a line of SQL in sight!

  WomanWithCat:flask grahamellis$ python alch002
  users
  <User(name='ed', fullname='Ed Jones', password='edspassword')>
  IdentitySet([<User(name='ed', fullname='Ed Jones', password='f8s7ccs')>])
  IdentitySet([<User(name='wendy', fullname='Wendy Williams', password='foobar')>, <User(name='mary', fullname='Mary Contrary', password='xxg527')>, <User(name='fred', fullname='Fred Flinstone', password='blah')>])
  1. -----------------------
  IdentitySet([])
  IdentitySet([])
  2. -----------------------
  [<User(name='ed', fullname='Ed Jones', password='f8s7ccs')>]
  1. ======================================
  (u'ed', u'Ed Jones')
  (u'wendy', u'Wendy Williams')
  (u'mary', u'Mary Contrary')
  (u'fred', u'Fred Flinstone')
  (<User(name='ed', fullname='Ed Jones', password='f8s7ccs')>, u'ed')
  (<User(name='wendy', fullname='Wendy Williams', password='foobar')>, u'wendy')
  (<User(name='mary', fullname='Mary Contrary', password='xxg527')>, u'mary')
  (<User(name='fred', fullname='Fred Flinstone', password='blah')>, u'fred')
  2. ======================================
  <User(name='wendy', fullname='Wendy Williams', password='foobar')>
  <User(name='mary', fullname='Mary Contrary', password='xxg527')>
  3. ======================================
  <User(name='ed', fullname='Ed Jones',password='f8s7ccs')>
  WomanWithCat:flask grahamellis$


But if we run it with echo on, you can see the underlying SQL - here's the end of the output with that switch thrown

  3. ======================================
  2015-10-14 07:37:47,723 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name, users.fullname AS users_fullname, users.password AS users_password
  FROM users
  WHERE users.name = ? AND users.fullname = ?
  2015-10-14 07:37:47,723 INFO sqlalchemy.engine.base.Engine ('ed', 'Ed Jones')
  <User(name='ed', fullname='Ed Jones', password='f8s7ccs')>


There's a very great deal to this ORM, including the echoing back to the database of changes made in memory (commit and rollback) and a whole extra library of functions to perform a wide range of tasks. The examples here are just "get you started" in the concepts!
(written 2015-10-14)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y113 - Python and SQL databases
  [4537] example of SQLite using a local database file through SQLalchemy - (2015-10-14)
  [4445] Graphing presentations in Python - huge data, numpy and matplotlib - (2015-02-28)
  [4436] Accessing a MySQL database from Python with mysql.connector - (2015-02-21)
  [4086] Cacheing class for Python - using a local SQLite database as a key/value store - (2013-05-14)
  [4024] SQL databases from Python - an SQLite example - (2013-03-02)
  [3139] Steering our Python courses towards wxPython, SQLite and Django - (2011-01-19)
  [3136] A framework with python - Django - first steps - (2011-01-17)
  [2790] Joining a MySQL table from within a Python program - (2010-06-02)
  [2786] Factory methods and SqLite in use in a Python teaching example - (2010-05-29)
  [2746] Model - View - Controller demo, Sqlite - Python 3 - Qt4 - (2010-04-29)
  [2745] Connecting Python to sqlite and MySQL databases - (2010-04-28)


Back to
Flask - complete MVC site to navigate around a data set
Previous and next
or
Horse's mouth home
Forward to
Json load from URL, recursive display, Python 3.4
Some other Articles
TransWilts public transport corridor
Flask and unittest - hello web app test world
Json load from URL, recursive display, Python 3.4
SQLAlchemy - first examples with a Python Object Relationship Mapping system
Flask - complete MVC site to navigate around a data set
Sessions in flask
Flask - first forms and responses with wtforms
Jinja2 - Flask templating
Requests in Flask
4735 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4535_SQL ... ystem.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat