Tag Archives: SqlAlchemy

SqlAlchemy: Connecting to pre-existing databases

Accessing databases with Python is a simple process. Python even provides a sqlite database library that’s built into the main distribution (since 2.5). My favorite way to access databases with Python is to use the 3rd party package, SqlAlchemy. SqlAlchemy is an object-relational mapper (ORM), which means that it takes SQL constructs and makes them more like the target language. In this case, you end up using Python syntax to execute SQL rather than straight SQL and you can use the same code to access multiple database backends (if you’re careful).

In this article, we’re going to look at how to use SqlAlchemy to connect to pre-existing databases. If my experience is any indication, you’ll probably be spending more time working with databases that you didn’t create than with ones that you did. This article will show you how to connect to them. Continue reading SqlAlchemy: Connecting to pre-existing databases

Another Step-by-Step SqlAlchemy Tutorial (part 2 of 2)

In the first part of this series, we went over what some might call the “SQL Expression” method of using SqlAlchemy to interact with your database. The theory behind this is that we should learn the less abstract way of doing things before we get to the higher level (and more abstract) methods. This is true in many math classes, like Calculus where you learn the long way to find the standard deviation of some calulation before you learn about the shortcut.

For the second half, we will be doing what some might say is the easy way to use SqlAlchemy. It is known as the “Object Relational” method and the official documentation actually starts with it. This methodology takes a little longer to set up initially, but in many ways, it is also much easier to follow. Continue reading Another Step-by-Step SqlAlchemy Tutorial (part 2 of 2)

Another Step-by-Step SqlAlchemy Tutorial (part 1 of 2)

A long time ago (circa 2007 if Google serves me right), there was a Python programmer named Robin Munn who wrote a really nice tutorial on SqlAlchemy. It was originally based on the 0.1 release, but updated for the newer 0.2. Then, Mr. Munn just disappeared and the tutorial was never updated. I have been kicking around the idea of releasing my own version of this tutorial for quite some time and finally decided to just do it. I hope you will find this article helpful as I found the original to be. Continue reading Another Step-by-Step SqlAlchemy Tutorial (part 1 of 2)