python通过elixir包操作mysql数据库实例代码
作者:y2701310012 发布时间:2024-01-26 04:12:16
标签:python,elixir,mysql数据库
本文研究的主要是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,结果为:
查看数据库,结果为:
来源:http://blog.csdn.net/y2701310012/article/details/40480421


猜你喜欢
- 所以就怀疑是否编码问题,或者文件权限问题,或者是不是函数不支持问题,经过排查发现原来是万网的L1主机不支持fsockopen,在文件uc_c
- 函数带括号和不带括号的区别1、不带括号时,调用的是这个函数本身 ,是整个函数体,是一个函数对象,不需等该函数执行完成2、带括号(此
- 本文实例为大家分享了Python实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下玩法:童年经典,普通模式没啥意思,小时候我们都是玩加速
- 1、下载pycharmpycharm是一种Python IDE,能够帮助我们在编写代码时提高效率。网上提供的有专业版和教育版之分。专业版是收
- Mac 安装Mysql有许多开发的小伙伴,使用的是mac,那么在mac上如何安装Mysql呢?这篇文章就给大家说说。1、首先,登陆Mysql
- 一、背景 今天闲着无事,写了一个小小的Python脚本程序,然后给同学炫耀的时候,发现每次都得拉着其他人过来看着自己的电脑屏幕,感觉不是很爽
- 正在看的ORACLE教程是:Oracle与SQL Server在企业应用的比较。在我供职的公司不仅仅拥有Oracle数据库,同时还拥有SQL
- 1. SELECT…FOR UPDATE 是什么?作用是什么?select for update 即排他锁,排他锁又称
- 一 前言前一段时间接二连三的出现开发人员在测试环境和生产误操作导致数据库误删除/更新,对DBA而言,回滚数据着实是一件头疼的事情,凡涉及到恢
- 本文介绍基于Python中seaborn模块,实现联合分布图绘制的方法。联合分布(Joint Distribution)图是一种查看两个或两
- 1.唯一性以下方法可以检查给定列表是否有重复的地方,可用set()的属性将其从列表中删除。x = [1,1,2,2,3,2,3,4,5,6]
- 本文参加新星计划人工智能(Pytorch)赛道:https://bbs.csdn.net/topics/613989052一、强大的 hub
- 无意中看到一段用Tkinter库写的放烟花的程序,就跟着跑了一遍。设计理念:通过让画面上一个粒子分裂为X数量的粒子来模拟 * 效果。粒子会发生
- 在“循环”一节,我们已经讨论了Python基本的循环语法。这一节,我们将接触更加灵活的循环方式。range()在Python中,for循环后
- 列表A是一个通过扩张对象浏览器(object explorer)中可编程性节点而建立的实例,选择存储过程,然后右击并选择新的存储过程。 许多
- 前言这篇博客针对《Python OpenCV识别行人入口进出人数统计》编写代码,功能包括了入口行人识别,人数统计。代码整洁,规则,易读。应用
- 图中图准备数据import matplotlib.pyplot as pltfig = plt.figure()x = [1, 2, 3,
- 前言众所周知,Python中没有所谓的main函数,但是网上经常有文章提到“ Python的main函数&rdq
- 在Google上搜一下,可以发现一大堆对ASP不好的评价,什么运行速度慢、异常处理机制不好、缺乏面向对象机制、开发效率低、漏洞多等等。为了让
- 当管理SQL Server内在的帐户和密码时,我们很容易认为这一切都相当的安全。毕竟,你的SQL Server系统被保护在防火墙里,而且还有