GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.fullstackpython.com — SQLAlchemy SQLAlchemySQLAlchemy (source code) is a well-regarded database toolkit and object-relational mapper (ORM) implementation written in Python. SQLAlchemy provides a generalized interface for creating and executing database-agnostic code without needing to write SQL statements. Why is SQLAlchemy a good ORM choice?SQLAlchemy isn't just an ORM- it also provides SQLAlchemy Core for performing database work that is abstracted from the implementation differences between PostgreSQL, SQLite, etc. In some ways, the ORM is a bonus to Core that automates commonly-required create, read, update and delete operations. SQLAlchemy can be used with or without the ORM features. Any given project can choose to just use SQLAlchemy Core or both Core and the ORM. The following diagram shows a few example configurations with various application software stacks and backend databases. Any of these configurations can be a valid option depending on what type of application you are coding.
A benefit many developers enjoy with SQLAlchemy is that it allows them to write Python code in their project to map from the database schema to the applications' Python objects. No SQL is required to create, maintain and query the database. The mapping allows SQLAlchemy to handle the underlying database so developers can work with their Python objects instead of writing bridge code to get data in and out of relational tables. SQLAlchemy is an implementation of the object-relational mapping (ORM) concept. Learn more in the data chapter or view all topics.
How does SQLAlchemy code compare to raw SQL?Below is an example of a SQLAlchemy model definition from the open source compare-python-web-frameworks project that uses SQLAlchemy with Flask and Flask-SQLAlchemy. class Contact(db.Model):
__tablename__ = 'contacts'
id = db.Column(db.Integer, primary_key=True)
first_name = db.Column(db.String(100))
last_name = db.Column(db.String(100))
phone_number = db.Column(db.String(32))
def __repr__(self):
return '<Contact {0} {1}: {2}>'.format(self.first_name,
self.last_name,
self.phone_number)
SQLAlchemy handles the table creation that otherwise we would have had to write a create table statement like this one to do the work: CREATE TABLE CONTACTS( ID INT PRIMARY KEY NOT NULL, FIRST_NAME CHAR(100) NOT NULL, LAST_NAME CHAR(100) NOT NULL, PHONE_NUMBER CHAR(32) NOT NULL, ); By using SQLAlchemy in our Python code, all records can be obtained with a
line like SQLAlchemy Extensions, Plug-ins and Related LibrariesTake a look at the SQLAlchemy extensions, plug-ins and related libraries page for a curated list of useful code libraries to use with SQLAlchemy. Using SQLAlchemy with Web FrameworksThere is no reason why you cannot use the SQLAlchemy library in any application that requires a database backend. However, if you are building a web app with Flask, Bottle or another web framework then take a look at the following extensions. They provide some glue code along with helper functions that can reduce the boilerplate code needed to connect your application's code with the SQLAlchemy library.
SQLAlchemy resourcesThe best way to get comfortable with SQLAlchemy is to dig in and write a database-driven application. The following resources can be helpful if you are having trouble getting started or are starting to run into some edge cases.
SQLAlchemy compared to other ORMsSQLAlchemy is one of many Python object-relational mapper (ORM) implementations. Several open source projects and articles are listed here to make it a bit easier to understand the differences between these implementations.
Open source code for learning SQLAlchemyMany open source projects rely on SQLAlchemy. A great way to learn how to properly work with this tool is to read the code that shows how those projects use SQLAlchemy. This section alphabetically lists these code examples by class and function in the SQLAlchemy code base. SQLAlchemy: Extensions, Plug-ins and Related Libraries & Example Projects and Codesqlalchemy.databases mysqlsqlalchemy.dialects mssql, mysql, oracle, postgresql, sqlitesqlalchemy.dialects.mysql pymysqlsqlalchemy.dialects.postgresql ARRAY, BIGINT, BIT, DOUBLE_PRECISION, ExcludeConstraint, INTEGER, JSON, TSVECTOR, array, json, pypostgresqlsqlalchemy.dialects.postgresql.base PGCompiler, PGIdentifierPreparer, PGTypeCompilersqlalchemy.dialects.postgresql.psycopg2 PGDialect_psycopg2sqlalchemy.dialects.sqlite pysqlitesqlalchemy.engine Connection, Engine, create_engine, default, urlsqlalchemy.engine.default DefaultDialectsqlalchemy.engine.interfaces ExecutionContextsqlalchemy.engine.result ResultMetaData, RowProxysqlalchemy.engine.strategies EngineStrategy, MockEngineStrategysqlalchemy.engine.url make_urlsqlalchemy.events SchemaEventTargetsqlalchemy.exc ArgumentError, DataError, DatabaseError, IntegrityError, InvalidRequestError, NoInspectionAvailable, NoSuchTableError, OperationalError, ProgrammingError, UnsupportedCompilationErrorsqlalchemy.ext compilersqlalchemy.ext.associationproxy AssociationProxysqlalchemy.ext.automap automap_basesqlalchemy.ext.compiler compilessqlalchemy.ext.declarative DeclarativeMeta, declarative_basesqlalchemy.ext.hybrid HYBRID_METHOD, HYBRID_PROPERTY, hybrid_method, hybrid_propertysqlalchemy.ext.mutable Mutablesqlalchemy.inspection inspectsqlalchemy.orm ColumnProperty, CompositeProperty, Load, Mapper, Query, RelationshipProperty, Session, SynonymProperty, aliased, attributes, backref, class_mapper, column_property, composite, interfaces, mapper, mapperlib, object_mapper, object_session, query, relationship, session, sessionmaker, strategiessqlalchemy.orm.attributes InstrumentedAttribute, QueryableAttribute, flag_modifiedsqlalchemy.orm.collections InstrumentedListsqlalchemy.orm.exc NoResultFound, UnmappedClassError, UnmappedInstanceErrorsqlalchemy.orm.interfaces MapperProperty, PropComparatorsqlalchemy.orm.mapper Mappersqlalchemy.orm.properties ColumnProperty, RelationshipPropertysqlalchemy.orm.query Query, QueryContextsqlalchemy.orm.relationships RelationshipPropertysqlalchemy.orm.session Session, object_sessionsqlalchemy.orm.util AliasedClass, AliasedInsp, identity_keysqlalchemy.pool NullPool, StaticPoolsqlalchemy.schema CheckConstraint, Column, CreateIndex, CreateTable, DDLElement, ForeignKey, ForeignKeyConstraint, Index, PrimaryKeyConstraint, Tablesqlalchemy.sql ClauseElement, Select, column, expression, extract, functions, operators, schema, select, sqltypes, tablesqlalchemy.sql.compiler SQLCompilersqlalchemy.sql.elements ColumnElement, Labelsqlalchemy.sql.expression ClauseElement, ColumnClause, ColumnElement, Executable, FunctionElement, UnaryExpressionsqlalchemy.sql.functions FunctionElement, GenericFunctionsqlalchemy.sql.naming convsqlalchemy.sql.schema Column, SchemaItemsqlalchemy.sql.sqltypes NULLTYPE, NullTypesqlalchemy.sql.util ClauseAdaptersqlalchemy.sql.visitors traversesqlalchemy.types BOOLEAN, Boolean, DATE, DATETIME, Date, DateTime, Enum, FLOAT, Float, INTEGER, Integer, Interval, NULLTYPE, NullType, String, TEXT, TIME, Text, Time, TypeEngine, UserDefinedType, to_instancesqlalchemy.util OrderedDict, OrderedSet, set_creation_order, symbol, topologicalsqlalchemy.util.langhelpers public_factory, symbolWhat would you like to learn about building Python web apps?
1. Introduction
2. Development Environments
3. Data
Relational Databases
PostgreSQL
MySQL
SQLite
Object-relational Mappers
SQLAlchemy
Peewee
Django ORM
Pony ORM
NoSQL Data Stores
Redis
MongoDB
Apache Cassandra
Neo4j
Data analysis
pandas
SciPy & NumPy
Data visualization
Bokeh
d3.js
Matplotlib
Markup Languages
Markdown
reStructuredText
Oracle
4. Web Development
5. Deployment
6. DevOps
Changelog
What Full Stack Means
About the Author
Future Directions
Page Statuses
...or view the full table of contents.
Full Stack Python
Full Stack Python
is an open book that explains concepts in plain language and provides
helpful resources for those topics.
Updates via Twitter & Facebook. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |