python中如何通過elixir包操作mysql數(shù)據(jù)庫-創(chuàng)新互聯(lián)

這篇文章主要介紹了python中如何通過elixir包操作mysql數(shù)據(jù)庫,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

站在用戶的角度思考問題,與客戶深入溝通,找到天峨網站設計與天峨網站推廣的解決方案,憑借多年的經驗,讓設計與互聯(lián)網技術結合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網站、網站設計、外貿營銷網站建設、企業(yè)官網、英文網站、手機端網站、網站推廣、域名注冊雅安服務器托管、企業(yè)郵箱。業(yè)務覆蓋天峨地區(qū)。

本文研究的主要是python通過elixir包操作mysql數(shù)據(jù)庫的相關實例,具體如下。

python操作數(shù)據(jù)庫有很多方法,下面介紹elixir來操作數(shù)據(jù)庫。elixir是對sqlalchemy lib的一個封裝,classes和tables是一一對應的,能夠一步定義classes,tables和mappers,支持定義多個primary key。

定義model.py

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 
#engine.execute("DROP DATABASE IF EXISTS elixir") 
engine.execute("CREATE DATABASE IF NOT EXISTS elixir") 
 
 
metadata.bind='mysql://root:root@localhost:3306/elixir' 
#metadata.bind.echo =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)>' % (self.title, self.year) 
 
class Person(Entity): 
  using_options(inheritance='multi') 
  using_options(tablename='person') 
 
  name = Field(Unicode(60)) 
 
  def __repr__(self): 
    return '<Person "%s">' % self.name 
 
 
class Director(Person): 
  using_options(inheritance='multi') 
  using_options(tablename='director') 
 
  movies = OneToMany('Movie') 
 
  def __repr__(self): 
    return '<Director "%s">' % self.name 
 
class Genre(Person): 
  using_options(inheritance='multi') 
  using_options(tablename='genre') 
 
  movies = ManyToMany('Movie') 
 
  def __repr__(self): 
    return '<Genre "%s">' % self.name 
 
class Actor(Person): 
  using_options(inheritance='multi') 
  using_options(tablename='actor') 
 
  movies = ManyToMany('Movie') 
 
  def __repr__(self): 
    return '<Actor "%s">' % self.name

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]) 
session.commit() 
 
 
m1 = Movie.query.filter_by(title=u"Alien").one() 
m2 = Movie.query.filter(Movie.year>1980).all() 
m3 = Movie.query.filter(Movie.director.has(name = u"Ridley Scott")).all() 
m4 = Movie.query.filter(Movie.director.has(Director.name.endswith(u"Scott"))).all() 
m5 = Movie.query.filter(Movie.genres.any(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 Class.query.filter_by(xxx).first 
q = Movie.query.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 " %(d.name) 
print m 
 
movies = q.order_by(sqlalchemy.desc(Movie.year)).all() 
print movies 
fro m in movies: 
  m.delete() 
session.commit()

執(zhí)行model.py,結果為:

python中如何通過elixir包操作mysql數(shù)據(jù)庫

查看數(shù)據(jù)庫,結果為:

python中如何通過elixir包操作mysql數(shù)據(jù)庫

感謝你能夠認真閱讀完這篇文章,希望小編分享的“python中如何通過elixir包操作mysql數(shù)據(jù)庫”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)成都網站設計公司,關注創(chuàng)新互聯(lián)成都網站設計公司行業(yè)資訊頻道,更多相關知識等著你來學習!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、網站設計器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網站欄目:python中如何通過elixir包操作mysql數(shù)據(jù)庫-創(chuàng)新互聯(lián)
網頁路徑:http://muchs.cn/article2/dpjeoc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、移動網站建設網站收錄、網站策劃、網站設計、電子商務

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網站建設