溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-06-18 10:07:32 來源:億速云 閱讀:146 作者:小新 欄目:開發(fā)技術(shù)

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

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

python操作數(shù)據(jù)庫有很多方法,下面介紹elixir來操作數(shù)據(jù)庫。elixir是對(duì)sqlalchemy lib的一個(gè)封裝,classes和tables是一一對(duì)應(yīng)的,能夠一步定義classes,tables和mappers,支持定義多個(gè)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,結(jié)果為:

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

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

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

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“python中如何通過elixir包操作mysql數(shù)據(jù)庫”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI