本文研究的主要是python通过elixir包操作mysql数据库的相关实例,具体如下。
python操作数据库有很多方法,下面介绍elixir来操作数据库。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()
执行model.py,结果为:
查看数据库,结果为:
总结
以上就是本文关于python通过elixir包操作mysql数据库实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]