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和shell实现的校验IP地址合法性脚本分享
Oct 23 Python
python+ffmpeg视频并发直播压力测试
Mar 06 Python
tensorflow学习笔记之mnist的卷积神经网络实例
Apr 15 Python
python 把文件中的每一行以数组的元素放入数组中的方法
Apr 29 Python
python中pip的安装与使用教程
Aug 10 Python
python获取全国城市pm2.5、臭氧等空气质量过程解析
Oct 12 Python
Python爬虫使用浏览器cookies:browsercookie过程解析
Oct 22 Python
使用tensorboard可视化loss和acc的实例
Jan 21 Python
使用Puppeteer爬取微信文章的实现
Feb 11 Python
pandas.DataFrame.drop_duplicates 用法介绍
Jul 06 Python
python爬取股票最新数据并用excel绘制树状图的示例
Mar 01 Python
python单元测试之pytest的使用
Jun 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
在PHP3中实现SESSION的功能(二)
2006/10/09 PHP
qq登录,新浪微博登录接口申请过程中遇到的问题
2014/07/22 PHP
php生成与读取excel文件
2016/10/14 PHP
PHP大文件及断点续传下载实现代码
2020/08/18 PHP
JQuery仿小米手机抢购页面倒计时效果
2014/12/16 Javascript
jQuery+PHP实现动态数字展示特效
2015/03/14 Javascript
jQuery制作简洁的图片轮播效果
2015/04/03 Javascript
JS+CSS实现仿触屏手机拨号盘界面及功能模拟完整实例
2015/05/16 Javascript
AspNet中使用JQuery boxy插件的确认框
2015/05/20 Javascript
JavaScript让Textarea支持tab按键的方法
2015/06/26 Javascript
javascript遇到html5的一些表单属性
2015/07/05 Javascript
javascript常用经典算法详解
2017/01/11 Javascript
基于JavaScript实现验证码功能
2017/04/01 Javascript
Angularjs 事件指令详细整理
2017/07/27 Javascript
Vue实现数字输入框中分割手机号码的示例
2017/10/10 Javascript
在 Angular-cli 中使用 simple-mock 实现前端开发 API Mock 接口数据模拟功能的方法
2018/11/28 Javascript
Node.js之readline模块的使用详解
2019/03/25 Javascript
vue中是怎样监听数组变化的
2020/10/24 Javascript
python实现ip查询示例
2014/03/26 Python
python实现文件分组复制到不同目录的例子
2014/06/04 Python
编写自定义的Django模板加载器的简单示例
2015/07/21 Python
Python实例一个类背后发生了什么
2016/02/09 Python
Python实现XML文件解析的示例代码
2018/02/05 Python
Django 配置多站点多域名的实现步骤
2019/05/17 Python
Python-copy()与deepcopy()区别详解
2019/07/12 Python
python使用QQ邮箱实现自动发送邮件
2020/06/22 Python
Python实现爬取网页中动态加载的数据
2020/08/17 Python
生产主管岗位职责
2013/11/10 职场文书
实习心得体会
2014/01/02 职场文书
感恩节活动策划方案
2014/05/16 职场文书
晋江市委常委班子四风问题整改工作方案
2014/10/26 职场文书
商场收银员岗位职责
2015/04/07 职场文书
2016年学校“3.12”植树节活动总结
2016/03/16 职场文书
2019辞职报告范本3篇!
2019/07/23 职场文书
小程序后台PHP版本部署运行 LNMP+WNMP
2021/04/01 Servers
Python如何利用正则表达式爬取网页信息及图片
2021/04/17 Python