Relational Databases: Object Relational Mappers - SQLObject BCHB524 2014 Lecture 22 11/14/2014BCHB524 - 2014 - Edwards.

Slides:



Advertisements
Similar presentations
Dimensional Modeling.
Advertisements

CHAPTER OBJECTIVE: NORMALIZATION THE SNOWFLAKE SCHEMA.
Introduction to Relational Databases Obtained from Portland State University.
PowerPoint Presentation for Dennis, Wixom & Tegarden Systems Analysis and Design Copyright 2001 © John Wiley & Sons, Inc. All rights reserved. Slide 1.
Data Management Design
PowerPoint Presentation for Dennis, Wixom & Tegarden Systems Analysis and Design Copyright 2001 © John Wiley & Sons, Inc. All rights reserved. Slide 1.
Information Technology in Organizations
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
Database – Part 2a Dr. V.T. Raja Oregon State University.
Managing Data Resources. File Organization Terms and Concepts Bit: Smallest unit of data; binary digit (0,1) Byte: Group of bits that represents a single.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
IST Databases and DBMSs Todd S. Bacastow January 2005.
Web-Applications: TurboGears II BCHB Lecture 26 12/03/2014BCHB Edwards.
Database System Concepts and Architecture Lecture # 3 22 June 2012 National University of Computer and Emerging Sciences.
Relational Databases: Object Relational Mappers – SQLObject II BCHB Lecture 23 11/20/2013BCHB Edwards.
Databases From A to Boyce Codd. What is a database? It depends on your point of view. For Manovich, a database is a means of structuring information in.
Data Access Patterns Some of the problems with data access from OO programs: 1.Data source and OO program use different data modelling concepts 2.Decoupling.
Database Technical Session By: Prof. Adarsh Patel.
Relational Databases: Basic Concepts BCHB Lecture 21 11/12/2014BCHB Edwards.
CTFS Workshop Shameema Esufali Suzanne Lao Data coordinators and technical resources for the network
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
Lecture2: Database Environment Prepared by L. Nouf Almujally & Aisha AlArfaj 1 Ref. Chapter2 College of Computer and Information Sciences - Information.
Object Persistence (Data Base) Design Chapter 13.
Object Persistence Design Chapter 13. Key Definitions Object persistence involves the selection of a storage format and optimization for performance.
Slide 1 Object Persistence Design Chapter 13 Alan Dennis, Barbara Wixom, and David Tegarden John Wiley & Sons, Inc. Slides by Fred Niederman Edited by.
ATADESAB. BATLE CORDER DLEIF Lesson objectives In this lesson you will learn some basic database terms and learn how a database is created.
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Set Containment Joins: The Good, The Bad and The Ugly Karthikeyan Ramasamy Jointly With Jignesh Patel, Jeffrey F. Naughton and Raghav Kaushik.
DataBase Management System What is DBMS Purpose of DBMS Data Abstraction Data Definition Language Data Manipulation Language Data Models Data Keys Relationships.
M1G Introduction to Database Development 5. Doing more with queries.
MANAGING DATA RESOURCES ~ pertemuan 7 ~ Oleh: Ir. Abdul Hayat, MTI.
1 CS 430 Database Theory Winter 2005 Lecture 2: General Concepts.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
Chapter 2 Introduction to Relational Model. Example of a Relation attributes (or columns) tuples (or rows) Introduction to Relational Model 2.
Chapter 2: Intro to Relational Model. 2.2 Example of a Relation attributes (or columns) tuples (or rows)
Indexes and Views Unit 7.
Managing Data Resources. File Organization Terms and Concepts Bit: Smallest unit of data; binary digit (0,1) Byte: Group of bits that represents a single.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Relational Databases: Basic Concepts BCHB Lecture 21 By Edwards & Li Slides:
Web-Applications: TurboGears II BCHB Lecture 26 12/7/2015BCHB Edwards.
Web-Applications: TurboGears BCHB Lecture 25 12/02/2015BCHB Edwards.
Connecting (relating) Data Tables to get Custom Records (Queries) Database Basics.
NoSQL: Graph Databases. Databases Why NoSQL Databases?
1 CS 430 Database Theory Winter 2005 Lecture 7: Designing a Database Logical Level.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Intro to Relational.
1 10 Systems Analysis and Design in a Changing World, 2 nd Edition, Satzinger, Jackson, & Burd Chapter 10 Designing Databases.
Week 2 Lecture The Relational Database Model Samuel ConnSamuel Conn, Faculty Suggestions for using the Lecture Slides.
Faeez, Franz & Syamim.   Database – collection of persistent data  Database Management System (DBMS) – software system that supports creation, population,
Introduction to Database Programming with Python Gary Stewart
Relational Databases: Basic Concepts
Web-Applications: TurboGears
Relational Databases: Object Relational Mappers - SQLObject
Chapter 2: Intro to Relational Model
Chapter 3: Intro to Relational Model
Chapter 2: Intro to Relational Model
Databases and Information Management
Relational Databases: Object Relational Mappers – SQLObject II
Databases and Information Management
Relational Databases: Basic Concepts
Relational Databases: Object Relational Mappers - SQLObject
Relational Databases: Basic Concepts
Chapter 2: Intro to Relational Model
Relational Databases: Object Relational Mappers – SQLObject II
Relational Databases: Object Relational Mappers – SQLObject II
Relational Databases: Object Relational Mappers - SQLObject
Indexes and Performance
Presentation transcript:

Relational Databases: Object Relational Mappers - SQLObject BCHB Lecture 22 11/14/2014BCHB Edwards

11/14/2014BCHB Edwards2 Outline Object relational mappers Tables as classes, rows as instances Advantages & disadvantages Minimal SQLObject example Legacy databases Exercises

11/14/2014BCHB Edwards3 Relational Databases Store information in a table Rows represent items Columns represent items' properties or attributes NameContinentRegionSurface AreaPopulationGNP BrazilSouth America IndonesiaAsiaSoutheast Asia IndiaAsiaSouthern and Central Asia ChinaAsiaEastern Asia PakistanAsiaSouthern and Central Asia United StatesNorth America

11/14/2014BCHB Edwards4... as Objects Objects have data members or attributes. Store objects in a list or iterable. Abstract away details of underlying RDBMS c1 = Country() c1.name = 'Brazil' c1.continent = 'South America' c1.region = 'South America' c1.surfaceArea = c1.population = c1.gnp = # initialize c2,..., c6 countryTable = [ c1, c2, c3, c4, c5, c6 ] for cnty in countryTable: if cnty.population > : print cnty.name, cnty.population

11/14/2014BCHB Edwards5 Pros and Cons Pros: Learn one language Ease of development Simplified joins One set of data-types Easy storage of higher-level objects Can apply the power of python as necessary Abstract away RDBMS Distribute CPU load Cons: Execution speed Sometimes forced into poor strategies Optimal SQL construct may be impossible Tend not to take advantage of RDBMS quirks. Can be difficult to apply to legacy databases

11/14/2014BCHB Edwards6 SQLObject Just one of many object-relational mappers Each tool makes different tradeoffs in Table/row/attribute abstraction How much SQL you need to know Overhead Ease of adapting to legacy databases SQLObject is almost completely devoid of SQL and is almost entirely "objecty". See

11/14/2014BCHB Edwards7 Minimal SQLObject Example: Define the database model (model.py) from sqlobject import * import os.path dbfile = 'myworld.db3' # Magic formatting for database URI conn_str = os.path.abspath(dbfile) conn_str = 'sqlite:'+ conn_str sqlhub.processConnection = connectionForURI(conn_str) class Country(SQLObject): name = StringCol() continent = StringCol() region = StringCol() surfaceArea = FloatCol() population = IntCol() gnp = FloatCol()

11/14/2014BCHB Edwards8 Minimal SQLObject Example: Populate the database from model import Country # Initialize the table Country.createTable() # Add some rows c = Country(name="Brazil", continent="South America", region="South America", surfaceArea= , population= , gnp=776739) c = Country(name="China", continent="Asia", region="Eastern Asia", surfaceArea= , population= , gnp=982268) #... c = Country(name="United States", continent="North America", region="North America", surfaceArea= , population= , gnp= ) # Retrieve and print all countries for c in Country.select(): print c.id, c.name, c.continent, c.gnp

11/14/2014BCHB Edwards9 Minimal SQLObject Example: Access/Change the database from model import Country # Change country #6 c = Country.get(6) c.name = 'United States of America' # Retrieve and print all countries for c in Country.select(): print c.id, c.name, c.continent, c.gnp

11/14/2014BCHB Edwards10 Minimal SQLObject Example: Access the rows as objects from model import Country # Select countries with more than 500,000,000 in population for c in Country.select(Country.q.population >= ): print "A:", c.id, c.name, c.population # Select countries that start with 'U' for c in Country.select(Country.q.name.startswith("U")): print "B:", c.id, c.name, c.population # Lookup by id, exactly 1 country with each id c = Country.get(5) print "C:", c.id, c.name, c.population # Get exception for bad id # c = Country.get(100) # Shortcut for select, countries with continent == 'Asia' for c in Country.selectBy(continent = 'Asia'): print "D:", c.id, c.name, c.population

11/14/2014BCHB Edwards11 Legacy databases If the legacy database is well-structured, SQLObject can figure out (most of) the definitions If there is no id column... Need to tell SQLObject what to use for the ID. May need to specify the id at instantiation time. Have to fill in MultipleJoins and ForeignKeys yourself Need to declare which columns in two different tables should correspond. Enables SQLObject to make relationships explicit Enables SQLObject to turn joins into lists

11/14/2014BCHB Edwards12 Legacy databases from sqlobject import * import os.path dbfile = 'taxa.db3' conn_str = os.path.abspath(dbfile) conn_str = 'sqlite:'+ conn_str sqlhub.processConnection = connectionForURI(conn_str) class Taxonomy(SQLObject): class sqlmeta: idName = "tax_id" fromDatabase = True names = MultipleJoin('Name', joinColumn="tax_id") class Name(SQLObject): class sqlmeta: fromDatabase = True taxa = ForeignKey('Taxonomy', dbName="tax_id")

11/14/2014BCHB Edwards13 Legacy databases # Set up data-model from model import * # get homo sapiens hs1 = Taxonomy.get(9606) # select the Taxonomy object # with scientific name Homo sapiens hs2 = Taxonomy.selectBy(scientificName='Homo sapiens')[0] # get the name human try: hsname = Name.selectBy(name='human')[0] except IndexError: print "Can't find name 'human'" sys.exit(1) # get the Taxonomy object from the Name object # Uses the magic Foreign Key attribute hs3 = hsname.taxa # hs1, hs2, hs3 the same! print hs1 print hs2 print hs3

11/14/2014BCHB Edwards14 Legacy databases # Set up data-model from model import * # get homo sapiens hs = Taxonomy.get(9606) # find rows in the Name table with taxa the same as hs # Use ForeignKey to create condition, equality test # between objects condition = (Name.q.taxa == hs) for n in Name.select(condition): print n # find rows in the Name table corresonding to hs # Easy shortcut, using MultipleJoin iterable for n in hs.names: print n.name, "|", n.nameClass # More general conditions condition = Name.q.name.startswith('Da') for n in Name.select(condition): print n.name, "|", n.nameClass

11/14/2014BCHB Edwards15 Exercises Read through the SQLObject documentation Write a python program using SQLObject to lookup the scientific name for a user-supplied organism name.