SQLAlchemy and Elixir (in a few minutes) Neil Blakey-Milner

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
POSTGRESQL DUNGEON WITH TABLE INHERITANCE AND CONSTRAINTS Edel Sherratt.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Greg Riccardi Florida State University. Using SQL to Manipulate Database Content and Structure How to create queries in SQL –Simple select statements.
Chapter Information Systems Database Management.
Data Quality Class 5. Goals Project Data Quality Rules (Continued) Example Use of Data Quality Rules.
Movies length titleyearfilmType Voices isa Cartoons isa MurderMystery weapon toStar Our Movie Example.
Relational Database Design and MySQL
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
Database Systems Lecture 5 Natasha Alechina
Databases Dan Otero Alex Loddengaard
MySql In Action Step by step method to create your own database.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
PHP1-1 PHP & SQL Xingquan (Hill) Zhu
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Slide 8-1 CHAPTER 8 Using Databases with PHP Scripts: Using MySQL Database with PHP.
Database testing Prepared by Saurabh sinha. Database testing mainly focus on: Data integrity test Data integrity test Stored procedures test Stored procedures.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
1 Overview of Databases. 2 Content Databases Example: Access Structure Query language (SQL)
© D. Wong  Indexes  JDBC  JDBC in J2EE (Java 2 Enterprise Edition)
Shaowen Wang CyberInfrastructure and Geospatial Information Laboratory (CIGI) Department of Geography and National Center for Supercomputing Applications.
Web Services Week 8 Aims: –Using web services as front ends to databases Objectives: –Review of relational databases –Connecting to and querying databases.
1 Advanced Databases (CM036): Lecture # 5 ( Object-Relational and Nested-Relational Databases) Introduction to Object-Relational features of Oracle 9i.
9 Persistence - SQLite CSNB544 Mobile Application Development Thanks to Utexas Austin.
Relational Database. Database Management System (DBMS)
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
CS 1308 Computer Literacy and the Internet
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
DAT602 Database Application Development Lecture 2 Review of Relational Database.
Index Example From Garcia-Molina, Ullman, and Widom: Database Systems, the Complete Book pp
What’s a database? Data stored in a structured format that lends itself to easy manipulation and recall.
Relational Databases Charles Severance. Unless otherwise noted, the content of this course material is licensed under a Creative Commons Attribution 3.0.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
* Database is a group of related objects * Objects can be Tables, Forms, Queries or Reports * All data reside in Tables * A Row in a Table is a record.
Relational Databases and SQLite
Jennifer Widom Relational Databases The Relational Model.
Relational Database Design and MySQL Charles Severance
Working with MySQL A290/A590, Fall /07/2014.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Databases Introduction - concepts. Concepts of Relational Databases.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Introduction to Database Programming with Python Gary Stewart
Relational Databases Charles Severance Relational Databases Relational databases model data by storing.
Big Data Yuan Xue CS 292 Special topics on.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Chapter 5 Introduction to SQL.
CS320 Web and Internet Programming SQL and MySQL
Principles of GIS Fundamental database concepts – II Shaowen Wang
Insert, Update and the rest…
Relational Databases and SQLite
Chapter 12 Information Systems.
CS1222 Using Relational Databases and SQL
SQLAlchemy Using python to interact with SQL databases
Structured Query Language (Data definition Language)
CS1222 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
CS1222 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
Presentation transcript:

SQLAlchemy and Elixir (in a few minutes) Neil Blakey-Milner

Installing SQLAlchemy easy_install SQLAlchemy Oh, and a DBAPI2 provider (pysqlite3 part of Python 2.5)

Connect to the database... from sqlalchemy import * db = create_engine('sqlite:///tutorial.db') metadata = BoundMetaData(db)

Declaring a table >>> users_table = Table('users', metadata,... Column('user_id', Integer, primary_key=True),... Column('user_name', String(40)),... Column('password', String(10))... )

Creating a table >>> metadata.engine.echo = True >>> users_table.create() CREATE TABLE users ( user_id INTEGER NOT NULL, user_name VARCHAR(40), password VARCHAR(10), PRIMARY KEY (user_id) )

Declaring a table from DB users_table = Table('users', metadata, autoload=True)

Inserting into a table >>> i = users_table.insert() >>> i.execute(user_name='Mary', password='secure') INSERT INTO users (user_name, password) VALUES (?, ?) ['Mary', 'secure'] COMMIT

Inserting multiple lines >>> i.execute({'user_name':'Tom'}, {'user_name':'Fred'}, {'user_name':'Harry'}) INSERT INTO users (user_name) VALUES (?) [['Tom'], ['Fred'], ['Harry']] COMMIT

Selecting >>> r = users_table.select(users_table.c.user_name == 'Harry').execute() SELECT users.user_id, users.user_name, users.password FROM users WHERE users.user_name = ? ['Harry'] >>> print r.fetchone() (4, u'Harry', None)

Table relationships >>> _addresses_table = Table(' _addresses',... metadata,... Column('address_id', Integer, primary_key=True),... Column(' _address', String(100), nullable=False),... Column('user_id', Integer,... ForeignKey('users.user_id')))

Table relationships (2) >>> _addresses_table.create() CREATE TABLE _addresses ( address_id INTEGER NOT NULL, _address VARCHAR(100) NOT NULL, user_id INTEGER, PRIMARY KEY (address_id), FOREIGN KEY(user_id) REFERENCES users (user_id) )

Selecting across tables (1) >>> _addresses_table.insert().execute(... 'user_id':2},... 'user_id':1}) INSERT INTO _addresses ( _address, user_id) VALUES (?, ?) 2], 1]] COMMIT

Selecting across tables (2) >>> r = users_table.join(... _addresses_table... ).select(... order_by = users_table.c.user_id... ).execute() SELECT users.user_id, users.user_name, users.password, _addresses.address_id, _addresses. _address, _addresses.user_id FROM users JOIN _addresses ON users.user_id = _addresses.user_id ORDER BY users.user_id

Selecting across tables (3) >>> print [row for row in r] [(1, u'Mary', u'secure', 2, 1), (2, u'Tom', None, 1, 2)]

Mappers SQLAlchemy allows you to associate a Table object with a Python class >>> class User(object):... pass >>> mapper(User, users_table) >>> ed = User() >>> ed.user_name = 'Ed' >>> ed.password = 'edspassword' >>> session.save(ed)

Elixir Elixir allows you to combine the steps, and use a declarative “domain specific language” to define the table. class User(Entity): has_field('user_id', Integer, primary_key = True) has_field('user_name', String(40)) has_field('password', String(10))

Unit of work (1) >>> mary = User.get_by(user_name = "Mary") >>> harry = User.get_by(user_name = "Harry") >>> fred = User.get_by(user_name = “Fred”) >>> mary.password = “marysnewpassword” >>> harry.password = “harrysnewpassword” >>> fred.delete() >>> ed = User(user_name = “Ed”, password = “ed”)

Unit of Work (2) >>> objectstore.flush() BEGIN UPDATE user SET password=? WHERE user.user_id = ? ['marysnewpassword', 1] UPDATE user SET password=? WHERE user.user_id = ? ['harrysnewpassword', 2] INSERT INTO user (user_name, password) VALUES (?, ?) ['ed', 'ed'] DELETE FROM user WHERE user.user_id = ? [3] COMMIT

Elixir Relationships (1) class Movie(Entity): has_field('title', Unicode(30)) has_field('year', Integer) has_field('description', Unicode) belongs_to('genre', of_kind='Genre') def __repr__(self): return ' ' % (self.title, self.year) class Genre(Entity): has_field('name', Unicode(15)) has_many('movies', of_kind='Movie') def __repr__(self): return ' ' % self.name

Elixir Relationships (2) scifi = Genre('Science Fiction') action = Genre('Action') alien = Movie(title="Alien", year=1979) starwars = Movie(title="Star Wars", year=1977) brunner = Movie(title="Blade Runner", year=1982) frcon = Movie(title="The French Connection", year=1971) prof = Movie(title="The Professional", year=1994) scifi.movies.append(alien) scifi.movies.append(starwars) scifi.movies.append(brunner) action.movies.append(frcon) action.movies.append(prof)

Elixir Relationships (3) CREATE TABLE genre ( name VARCHAR(15), id INTEGER NOT NULL, PRIMARY KEY (id) ) CREATE TABLE movie ( id INTEGER NOT NULL, year INTEGER, description TEXT, title VARCHAR(30), genre_id INTEGER, PRIMARY KEY (id), CONSTRAINT movie_genre_fk FOREIGN KEY(genre_id) REFERENCES genre (id) ) CREATE INDEX ix_movie_genre_id ON movie (genre_id)

Elixir Relationships (4) BEGIN INSERT INTO genre (name) VALUES (?) ['Science Fiction'] INSERT INTO genre (name) VALUES (?) ['Action'] INSERT INTO movie (year, description, title, genre_id) VALUES (?, ?, ?, ?) [1979, None, 'Alien', 1] INSERT INTO movie (year, description, title, genre_id) VALUES (?, ?, ?, ?) [1977, None, 'Star Wars', 1] INSERT INTO movie (year, description, title, genre_id) VALUES (?, ?, ?, ?) [1982, None, 'Blade Runner', 1] INSERT INTO movie (year, description, title, genre_id) VALUES (?, ?, ?, ?) [1971, None, 'The French Connection', 2] INSERT INTO movie (year, description, title, genre_id) VALUES (?, ?, ?, ?) [1994, None, 'The Professional', 2] COMMIT

Elixir Relationships (5) class Movie(Entity): has_field('title', Unicode(30)) has_field('year', Integer) has_field('description', Unicode) has_and_belongs_to_many('genre', of_kind='Genre') def __repr__(self): return ' ' % (self.title, self.year) class Genre(Entity): has_field('name', Unicode(15)) has_and_belongs_to_many('movies', of_kind='Movie') def __repr__(self): return ' ' % self.name

Elixir Relationships (6) scifi = Genre(name = 'Science Fiction') action = Genre(name = 'Action') thriller = Genre(name = 'Thriller') crime = Genre(name = 'Crime') alien = Movie(title="Alien", year=1979) starwars = Movie(title="Star Wars", year=1977) brunner = Movie(title="Blade Runner", year=1982) frcon = Movie(title="The French Connection", year=1971) prof = Movie(title="The Professional", year=1994) manch = Movie(title="Manchurian Candidate", year=1962)

Elixir Relationships (7) scifi.movies.append(alien) scifi.movies.append(starwars) scifi.movies.append(brunner) thriller.movies.append(alien) frcon.genres.append(action) frcon.genres.append(crime) frcon.genres.append(thriller) prof.genres.extend([action, crime]) manch.genres.extend([action, thriller])

Elixir Relationship (8) CREATE TABLE genre ( name VARCHAR(15), id INTEGER NOT NULL, PRIMARY KEY (id) ) CREATE TABLE movie ( id INTEGER NOT NULL, year INTEGER, description TEXT, title VARCHAR(30), PRIMARY KEY (id) ) CREATE TABLE movie_genres__genre_movies ( genre_id INTEGER, movie_id INTEGER, CONSTRAINT genre_movies_fk FOREIGN KEY(genre_id) REFERENCES genre (id), CONSTRAINT movie_genres_fk FOREIGN KEY(movie_id) REFERENCES movie (id) )

Elixir Relationship (9) BEGIN INSERT INTO movie (year, description, title) VALUES (?, ?, ?) [1979, None, 'Alien']... INSERT INTO movie (year, description, title) VALUES (?, ?, ?) [1994, None, 'Manchurian Candidate'] INSERT INTO genre (name) VALUES (?) ['Science Fiction']... INSERT INTO genre (name) VALUES (?) ['Crime'] INSERT INTO movie_genres__genre_movies (genre_id, movie_id) VALUES (?, ?) [[2, 4], [4, 4], [3, 4], [2, 5], [4, 5], [2, 6], [3, 6]] INSERT INTO movie_genres__genre_movies (genre_id, movie_id) VALUES (?, ?) [[3, 1], [1, 1], [1, 2], [1, 3]] COMMIT

More queries (1) >>> Movie.select(Movie.join_to('genres') & (Genre.c.name == "Science Fiction")) SELECT movie.title AS movie_title, movie.description AS movie_description, movie.id AS movie_id, movie.year AS movie_year FROM movie, movie_genres__genre_movies, genre WHERE (movie.id = movie_genres__genre_movies.movie_id AND genre.id = movie_genres__genre_movies.genre_id) AND genre.name = ? ORDER BY movie.oid ['Science Fiction'] [,, ]

More queries (2) >>> Movie.select(Movie.join_to('genres') & (Genre.c.name == "Science Fiction") & (Movie.c.year < 1980)) SELECT movie.title AS movie_title, movie.description AS movie_description, movie.id AS movie_id, movie.year AS movie_year FROM movie, movie_genres__genre_movies, genre WHERE ((movie.id = movie_genres__genre_movies.movie_id AND genre.id = movie_genres__genre_movies.genre_id) AND genre.name = ?) AND movie.year < ? ORDER BY movie.oid ['Science Fiction', 1980] [, ]

More queries (3) >>> from sqlalchemy import and_ >>> Movie.select(and_(Movie.join_to('genres'), Genre.c.name == "Science Fiction", Movie.c.year.between(1978, 1982))) SELECT movie.title AS movie_title, movie.description AS movie_description, movie.id AS movie_id, movie.year AS movie_year FROM movie, movie_genres__genre_movies, genre WHERE (movie.id = movie_genres__genre_movies.movie_id AND genre.id = movie_genres__genre_movies.genre_id) AND genre.name = ? AND movie.year BETWEEN ? AND ? ORDER BY movie.oid ['Science Fiction', 1978, 1982] [, ]

More queries (4) def getTags(limit = None, min = None): s = select([Tag.c.tag_id, func.count(Post.c.post_id)], Tag.join_to('posts'), group_by=[Tag.c.tag_id], order_by=[desc(func.count(Post.c.post_id))]) if limit: s.limit = limit if min: s.having = func.count(Post.c.post_id) > min r = s.execute() for tag_id, num in r: yield Tag.get(tag_id), num

Other features Deferred loading of class attributes (ie, columns in the table) Inheritance – single and multiple, polymorphic or not Mapping a class against an arbitrary select clause Multiple column primary keys Ordered lists of associated items Extensible association relations