This article studies the main python through the elixir package to operate mysql database related examples, as follows.
There are many ways to manipulate the database in python, the following is an introduction to elixir to manipulate the database. elixir is a package for the sqlalchemy lib, classes and tables are one-to-one correspondence, able to define classes, tables and mappers in one step, support for the definition of multiple primary key.
define
from elixir import sqlalchemy from elixir import * engine =sqlalchemy.create_engine('mysql://root:root@localhost/') #the first root is the user, and the sencond root is the password #("DROP DATABASE IF EXISTS elixir") ("CREATE DATABASE IF NOT EXISTS elixir") ='mysql://root:root@localhost:3306/elixir' # =True class Movie(Entity): using_options(tablename='movies') title = Field(Unicode(30),primary_key = True) year = Field(Integer, primary_key = True) description = Field(UnicodeText) director = ManyToOne('Director') genres = ManyToMany('Genre') actor = ManyToMany('Actor') def __repr__(self): return '<Move "%s" (%d)>' % (, ) class Person(Entity): using_options(inheritance='multi') using_options(tablename='person') name = Field(Unicode(60)) def __repr__(self): return '<Person "%s">' % class Director(Person): using_options(inheritance='multi') using_options(tablename='director') movies = OneToMany('Movie') def __repr__(self): return '<Director "%s">' % class Genre(Person): using_options(inheritance='multi') using_options(tablename='genre') movies = ManyToMany('Movie') def __repr__(self): return '<Genre "%s">' % class Actor(Person): using_options(inheritance='multi') using_options(tablename='actor') movies = ManyToMany('Movie') def __repr__(self): return '<Actor "%s">' %
model_test.py
from model import * # setup_all(True) is equal to the following two staps: setup_all() # create sqlalchemy table object as mapper object for the class create_all() # take all table objcts and create real tables by issuing SQL statements on the databse. Actor1 = Actor(name=u"lvliang") scifi = Genre(name = u"Science-Fiction") rscott = Director(name = u"Ridley Scott") glucas = Director(name = u"George Lucas") alien = Movie(title = u"Alien", year = 1979, director=rscott, genres=[scifi, Genre(name=u"Horror")], actor = [Actor1]) brunner = Movie(title = u"Blade Runner", year = 1982, director = rscott, genres=[scifi]) swars = Movie(title = u"Star Wars", year = 1977, director = glucas, genres=[scifi]) () m1 = .filter_by(title=u"Alien").one() m2 = (>1980).all() m3 = ((name = u"Ridley Scott")).all() m4 = (((u"Scott"))).all() m5 = ((name = u"Horror")).all() print m1 print m2 print m3 print m4 print m5 d = Director.get_by(name = u"Ridley Scott") # Class.get_by(xxx) is a shortcut for .filter_by(xxx).first q = .filter_by(director = d) #get all movies directed by director d m = q.filter_by(year = 1979).all() print "Movie direct by %s in year 1979 are " %() print m movies = q.order_by(()).all() print movies fro m in movies: () ()
Execution with results:
Viewing the database results in:
summarize
Above is this article on python through the elixir package to operate mysql database example code of the entire content, I hope to help you. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out. Thank you for the support of friends on this site!