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 selenium 三种等待方式详解(必会)
Sep 15 Python
详解Python 数据库 (sqlite3)应用
Dec 07 Python
python实现读取大文件并逐行写入另外一个文件
Apr 19 Python
Python 实现遥感影像波段组合的示例代码
Aug 04 Python
python用线性回归预测股票价格的实现代码
Sep 04 Python
Python爬虫实现的根据分类爬取豆瓣电影信息功能示例
Sep 15 Python
python日志模块logbook使用方法
Sep 19 Python
Python序列对象与String类型内置方法详解
Oct 22 Python
python 根据列表批量下载网易云音乐的免费音乐
Dec 03 Python
python利用appium实现手机APP自动化的示例
Jan 26 Python
Python实现随机爬山算法
Jan 29 Python
python tkinter实现下载进度条及抖音视频去水印原理
Feb 07 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
E路文章系统PHP
2006/12/11 PHP
PHP写的获取各搜索蜘蛛爬行记录代码
2012/08/21 PHP
解析phpstorm + xdebug 远程断点调试
2013/06/20 PHP
php+redis实现多台服务器内网存储session并读取示例
2017/01/12 PHP
PHPExcel中文帮助手册|PHPExcel使用方法(分享)
2017/06/09 PHP
Laravel框架实现的rbac权限管理操作示例
2019/01/16 PHP
IE8 原生JSON支持
2009/04/13 Javascript
jQuery lazyload 的重复加载错误以及修复方法
2010/11/19 Javascript
js数组Array sort方法使用深入分析
2013/02/21 Javascript
javascript中加号(+)操作符的一些神奇作用
2014/06/06 Javascript
js实现鼠标感应向下滑动隐藏菜单的方法
2015/02/20 Javascript
基于jQuery实现点击弹出层实例代码
2016/01/01 Javascript
AngularJs 弹出模态框(model)
2016/04/07 Javascript
javascript中this指向详解
2016/04/23 Javascript
vue父组件中获取子组件中的数据(实例讲解)
2017/09/27 Javascript
JavaScript去掉数组重复项的方法分析【测试可用】
2018/07/19 Javascript
用POSTMAN发送JSON格式的POST请求示例
2018/09/04 Javascript
vue 配置多页面应用的示例代码
2018/10/22 Javascript
微信小程序实现图片翻转效果的实例代码
2019/09/20 Javascript
JS实现音量控制拖动
2020/01/15 Javascript
jQuery中getJSON跨域原理的深入讲解
2020/09/02 jQuery
零基础写python爬虫之爬虫的定义及URL构成
2014/11/04 Python
Python中统计函数运行耗时的方法
2015/05/05 Python
python编程开发之类型转换convert实例分析
2015/11/13 Python
Python输出汉字字库及将文字转换为图片的方法
2016/06/04 Python
Python3.6 Schedule模块定时任务(实例讲解)
2017/11/09 Python
Python设置在shell脚本中自动补全功能的方法
2018/06/25 Python
python感知机实现代码
2019/01/18 Python
Django的models中on_delete参数详解
2019/07/16 Python
Python找出列表中出现次数最多的元素三种方式
2020/02/24 Python
专门经营化妆刷的美国彩妆品牌:Sigma Beauty
2017/09/11 全球购物
竞聘演讲稿范文
2014/01/12 职场文书
学生乘坐校车安全责任书
2015/05/11 职场文书
vue3如何优雅的实现移动端登录注册模块
2021/03/29 Vue.js
详解在OpenCV中如何使用图像像素
2022/03/03 Python
安装Ruby和 Rails的详细步骤
2022/04/19 Ruby