Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
This is now an archive web site.
Some is still relevant as at November 2026 but some is purely of historic interest.


Lisa and I (Graham) are now fully retired from IT training.We have made many friends over 30 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Apache, Linux and Solaris/SunOS too. Our training notes are out of date, but with upward compatability some examples remain operational and relevant. You are welcome to make use of them "as seen", at your own risk. We now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We remain active, enjoying the times that we are retired but still healthy enough in mind and body to do things!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
SQLAlchemy - first examples with a Python Object Relationship Mapping system (written 2015-10-14)

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


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
4759 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, 96 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. So much so, and it's so long ago that we are retired

Link to Ezine home page (for reading).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2026: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: +44 (0)1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net

PAGE: https://www.wellho.info//mouthwork/4535_SQLAlchemy-first-examples-with-a-Python-Object-Relationship-Mapping-system.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb