SQL Alchemy Advanced Python Nbn 2007

  • Upload
    apmapm

  • View
    235

  • Download
    0

Embed Size (px)

Citation preview

  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    1/25

    SQLAlchemy

    http://goforward/http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    2/25

    Just like SQLObject

    Third party library Object relational manager

    Define database tables, connect (map)classes to tables

    Table rows are objects / instances What you do to an object, you do to a row

    Phrase queries in a morepythonic(less SQL-ish) way

    Hide database differences

    The database disappears ...

    http://goforward/http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    3/25

    Making an engine

    Connecting is slightly more complicated than SQLObject:

    Use a connection string of the formdriver://username:password@host:port/database

    Make adatabase enginewith create_engine

    encoding and convert_unicode can be used to convertcharacters for all transactions

    echo can be set to print raw SQL

    Use to make special metadata object

    from sqlalchemy import *

    conn_str = "mysql://dbuser:dbpass@localhost:8028/epi"

    engine = create_engine (conn_str, echo=True)

    metadata = MetaData (engine)

    http://goforward/http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    4/25

    Making a connection

    Call connect from engine Connection can execute raw SQL, like DB-API cursor

    objects.

    Dont forget to close() it!

    Actually we dont need to use the connection ...

    connection = engine.connect()

    result = connection.execute ("SELECT * FROM reports")

    # returns a sequence of dictionaries

    for row in result:print row[id], row[country]

    connection.close()

    http://goforward/http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    5/25

    Define tables

    Instantiating a Table object (not defining a class), defines thecorresponding table.

    The general form is Table (, , []+)

    Metadata binds table to database

    SampleTable = Table (sample,metadata,

    Column (ident, String(32), primary_key=True),

    Column (locn_lat, Float()),

    Column (locn_lon, Float()),

    Column (locn_method, String(32), default=EXACT),Column (country, String(64), nullable=False),

    Column (date_collected, Date()),

    )

    SampleTable.create()

    http://goforward/http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    6/25

    Table options

    Defining table is a call.

    Create table via .create().

    Can create all bound tables with .create all(). Links to actual database via metadata.

    Can take autoload parameter to work out columns fromdatabase.

    http://goforward/http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    7/25

    The awkward way of using the database

    Require a call on the table then an explicit execution

    query = SampleTable.insert()

    query.execute (ident=X47, locn_lat=34.1,

    locn_lon=-12.1)

    # ... or ...

    query = SampleTable.insert().execute (

    ident=X47, locn_lat=34.1, locn_lon=-12.1)

    http://goforward/http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    8/25

    Columns

    Oddly familiar:String (length) A string. If length isnt given, the field

    width is variable (Text)

    UnicodeCol A per StringCol, but with unicode strings.

    DateTime Also see Date and Time.Boolean May be implemented as a variety of types.

    Integer Duh.

    Numeric (precision, length) Precise float with the given overall

    width and number of figures after the point.FloatCol (precision) A float, with a level of precision.

    http://goforward/http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    9/25

    Column parameters

    primarykey False Is it a unique id for the row? You can have asmany as you want ...

    nullable True Value for this column. when created. Can becallable. If no default, must be provided (like NOT

    NULL).default Default value for column. Will call if callable.

    Opposite to SQLObject.

    oninsert Default value when entry created and no valuesupplied.

    onupdate Default value when entry updated and no valuesupplied.

    http://goforward/http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    10/25

    Column example

    A timestamped table for samples, where a source must beprovided, and a user-provided string serves as an id

    TimedTable = Table (sample,

    metadata,

    Column (ident, String(10), primarykey=True )Column (source, String(30), nullable=False)

    Column (time_created, DateTime(), de-

    fault=func.now()),

    Column(time_updated, DateTime(), onup-

    date=func.now()),)

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    11/25

    Autoload tables

    Derive columns from database

    users = Table(users, metadata, autoload=True)

    emails = Table(emails, metadata, autoload=True)

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    12/25

    func

    An answer to delayed execution / calling of functions: it binds afunction and can later call it with passed parameters.

    func.now () Wrapsnow() for later calling.func.max (mylist) Wraps max for later calling max (mylist).

    func.substr (users.c.name, 2, 1) Wraps for later callusers.c.name.substr (2, 1)

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    13/25

    Mappers

    Objects (in Python) dont automatically map to database tablesand rows. Instead they are mapped.

    users = Table (user_table, ...)

    class User (object):

    pass

    usermapper = mapper (User, users)

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    14/25

    Mapped classes

    Any mapped classes get the columns of the table as membersfor free.

    Mapped classes must be derived from object.

    mapper (, )

    You can use __init__ and subclass mapped objects Interaction takes place in the context of a session

    class Report (object):

    def init (self): # ...

    def setLocn (self, lat, lon): self.lat =lat self.lon = lon

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    15/25

    Sessions

    Sessions are devices for caching and saving the changes in objectsin tables. bind_to parameter is optional. List the objects to besaved.

    sess = create_session (bind_to=engine)# ... much later ...

    sess.save (changed_objs)

    # saves everything

    sess.flush()

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    16/25

    select

    select method of table searches rows

    query = .select ([.q.field ==

    ]+)

    query.execute()

    select_by()

    >>> query = report_table.select ()

    >>> str (query)

    SELECT * FROM report ...>>> results = query.execute()

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    17/25

    Results

    Query results are sequences of dictionary-like objects Results arelazyand can be sliced

    Use fetch syntax

    Can (should) close results

    >>> results = query.execute()>>> first_row = results.fetchone()

    >>> first_row[id]

    X47

    >>> first_row.id

    X47

    >>> second_row = results[1]

    >>> results.close()

    S

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    18/25

    Select options

    Use special c attribute to express clauses and conditions Can specify columns to return in select

    Can test fields

    some_cols_query = report_table.select ([re-

    port_table.c.id,

    report_table.c.country])

    country_query = report_table.select (re-

    port_table.c.country==Burma)

    next_query = report_table.select (re-port_table.c.country.in_ (

    North Korea, South Korea))

    l

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    19/25

    select operators

    ==, >, >=, etc.

    .like(), .startswith(), .endswith()

    .between(), and .in_()

    not_(), and_() and or_() (or ~, &, and |)

    +, -, *, /

    M l i l l

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    20/25

    Multiple selectors

    Use and_, op_ & not to combine conditions.

    query = report_table.select (and_ (

    (Report.c.diseaseStatus == None),

    or_ ((Report.c.country == Burma),

    (Report.c.country == Myanmar),

    ),

    )

    U i f i l t

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    21/25

    Using func in select

    Can use func to make arbitrary tests of fields.

    report_table.select (func.substr (report_table.c.country, 1) == A).execute()

    S l t i d l

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    22/25

    Select via mapped classes

    You can select classes instead of tables.

    Make queries on session and filter.

    report_table.select (func.substr (

    Report.c.country, 1) == A).execute()

    query = session.query (Report)

    reps = query.filter (Report.c.country == Burma)

    Odds d ds

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    23/25

    Odds and ends

    orm.clear_mappers(): clears all class mappings, usefulduring development

    Allegedly slow Have to explicitly manage sessions

    Many ways to do things (like SQLObject)

    MySQL tools

    http://www.aminus.org/blogs/index.php/fumanchu/2007/08/18/storm_sqlalchemy_and_geniusqlhttp://www.aminus.org/blogs/index.php/fumanchu/2007/08/18/storm_sqlalchemy_and_geniusqlhttp://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    24/25

    MySQL tools

    Graphical tools for administering MySQL. There are many - theseare only based on my personal experience.

    Aqua Data Studio ($?) CocoaMySQL (OSX, maybe the best)

    iSQL (cross platform)

    Resources

    http://find/http://goback/
  • 8/6/2019 SQL Alchemy Advanced Python Nbn 2007

    25/25

    Resources

    SQLAlchemy

    SQLAlchemy tutorial SQLAlchemy talk slides

    http://www.sqlalchemy.org/http://www.rmunn.com/sqlalchemy-tutorial/tutorial.htmlhttp://cleverdevil.org/elixirtalk/slides.pdfhttp://cleverdevil.org/elixirtalk/slides.pdfhttp://www.rmunn.com/sqlalchemy-tutorial/tutorial.htmlhttp://www.sqlalchemy.org/http://find/http://goback/