python通过elixir包操作mysql数据库实例代码


Posted in Python onJanuary 31, 2018

本文研究的主要是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数据库实例代码

查看数据库,结果为:

python通过elixir包操作mysql数据库实例代码

总结

以上就是本文关于python通过elixir包操作mysql数据库实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
Python中的多行注释文档编写风格汇总
Jun 16 Python
Python实现类的创建与使用方法示例
Jul 25 Python
用python实现的线程池实例代码
Jan 06 Python
Python操作MySQL数据库的方法
Jun 20 Python
python numpy 显示图像阵列的实例
Jul 02 Python
对pandas的层次索引与取值的新方法详解
Nov 06 Python
python中实现控制小数点位数的方法
Jan 24 Python
Python函数的参数常见分类与用法实例详解
Mar 30 Python
python实现从wind导入数据
Dec 03 Python
pytorch 常用函数 max ,eq说明
Jun 28 Python
python中lower函数实现方法及用法讲解
Dec 23 Python
Python合并pdf文件的工具
Jul 01 Python
Django视图和URL配置详解
Jan 31 #Python
Python编程求质数实例代码
Jan 31 #Python
Python及Django框架生成二维码的方法分析
Jan 31 #Python
Python进阶之尾递归的用法实例
Jan 31 #Python
简单的python协同过滤程序实例代码
Jan 31 #Python
Python进阶之递归函数的用法及其示例
Jan 31 #Python
Python tkinter事件高级用法实例
Jan 31 #Python
You might like
基于CI(CodeIgniter)框架实现购物车功能的方法
2018/04/09 PHP
laravel框架中间件 except 和 only 的用法示例
2019/07/12 PHP
jQuery 对Select的操作备忘记录
2011/07/04 Javascript
基于jQuery的模仿新浪微博时间的组件
2011/10/04 Javascript
左右悬浮可分组的网站QQ在线客服代码(可谓经典)
2012/12/21 Javascript
Knockout visible绑定使用方法
2013/11/15 Javascript
深入理解JavaScript系列(47):对象创建模式(上篇)
2015/03/04 Javascript
jQuery通过Ajax返回JSON数据
2015/04/28 Javascript
nodejs实现获取当前url地址及url各种参数值
2015/06/25 NodeJs
JS触摸屏网页版仿app弹窗型滚动列表选择器/日期选择器
2016/10/30 Javascript
jQuery简单自定义图片轮播插件及用法示例
2016/11/21 Javascript
React Router基础使用
2017/01/17 Javascript
Angular在一个页面中使用两个ng-app的方法
2017/02/20 Javascript
微信小程序实现打开内置地图功能【附源码下载】
2017/12/07 Javascript
node+koa2+mysql+bootstrap搭建一个前端论坛
2018/05/06 Javascript
微信小程序获取用户openid的实现
2018/12/24 Javascript
详解基于webpack&amp;gettext的前端多语言方案
2019/01/29 Javascript
Vue和React组件之间的传值方式详解
2019/01/31 Javascript
vue使用Font Awesome的方法步骤
2019/02/26 Javascript
详解微信小程序-canvas绘制文字实现自动换行
2019/04/26 Javascript
vue cli3 调用百度翻译API翻译页面的实现示例
2019/09/13 Javascript
ElementUI 修改默认样式的几种办法(小结)
2020/07/29 Javascript
初步探究Python程序的执行原理
2015/04/11 Python
Python生成随机数组的方法小结
2017/04/15 Python
Python cookbook(数据结构与算法)让字典保持有序的方法
2018/02/18 Python
Python3基于sax解析xml操作示例
2018/05/22 Python
Python爬虫包BeautifulSoup异常处理(二)
2018/06/17 Python
python实现写数字文件名的递增保存文件方法
2018/10/25 Python
Numpy 多维数据数组的实现
2020/06/18 Python
Python使用xlrd实现读取合并单元格
2020/07/09 Python
python包的导入方式总结
2021/03/02 Python
ColourPop美国官网:卡拉泡泡,洛杉矶彩妆品牌
2019/04/28 全球购物
莫斯科的韩国化妆品店:Sifo
2019/12/04 全球购物
会计人员演讲稿
2014/09/11 职场文书
检查机关领导群众路线教育实践活动个人整改措施
2014/10/28 职场文书
2014年办公室工作总结范文
2014/11/12 职场文书