python下的orm基本操作(1)--Mysql下的CRUD简单操作(含源码DEMO)
最近逐渐打算将工作的环境转移到ubuntu下,突然发现对于我来说,这ubuntu对于我这种上上网,收收邮件,写写博客,写写程序的时实在是太合适了,除了刚接触的时候会不怎么完全适应命令行及各种权限管理,apt-get命令相当的方便,各种原先在windows下各种奇怪错误在ubuntu下都没有出现了,好了,我就不说废话了,今天大致简单的介绍下python下的ORM to Mysql 的操作(注意:一定要看官网的文档!)
refer:http://docs.sqlalchemy.org/en/latest/orm/tutorial.html
一,准备环境
1.安装mysql-server (在此之前请准备好Python的环境)
2.安装mysql-python 这里有点坑,我直接使用apt-get命令没有成功,后来使用pip安装成功的
~$ pip install mysql-python
3.安装sqlalchemy
准备环境OK之后,安装sqlalchemy
~$ pip install sqlalchemy
如果你在第二步 pip install mysql-python 如图的类似的问题,这是需要安装connector for c 一些环境,如果你是x64的环境,请选中里面的x86,x64,都要安装

下载列表:http://dev.mysql.com/downloads/connector/c/6.0.html#downloads

参考的解决方案:http://stackoverflow.com/questions/1972259/cannot-open-include-file-config-win-h-no-such-file-or-directory-while-inst
环境都准备OK之后,我们来大致看一下如何使用sqlalchemy 的ORM
二,实际操作
1 创建engine对象
这里,engine类似我们的连接字符串,它指示了你会连接到哪种类型的数据库,用户名,密码,地址等,这里,我示例的是mysql数据库,
# -*- coding: UTF-8 -*- from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker engine = create_engine('mysql://root:password@127.0.0.1:3306/test?charset=utf8',echo=True)
关于其它类型的数据库的连接字符串的写法,参考:
http://docs.sqlalchemy.org/en/rel_1_0/core/engines.html#sqlalchemy.create_engine
2.定义映射关系
#declare a Mapping,this is the class describe map to table column
Base = declarative_base()
3.定义连接管理器
#connect session to active the action
Session = sessionmaker(bind=engine)
session = Session()
4.表结构与类结构映射
class Person(Base):
__tablename__ = 'Person'
Id = Column(Integer, primary_key=True,autoincrement=True)
Pname = Column(String,nullable=False,default='')
Address = Column(String,nullable=False,default='')
Age = Column(Integer,nullable=False,default=0) def __repr__(self):
return 'the info is ID %s Pname is %s Address is %s and Age is %s' % \
(self.Id, self.Pname, self.Address, self.Age)
根据以上的代码,就可以完整的操作Mysql了,完整的代码如下:
# -*- coding: UTF-8 -*- __author__ = 'Bruce'
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base #declare the connecting to the server
engine = create_engine('mysql://account:password@127.0.0.1:3306/test?charset=utf8',echo=False) #declare a Mapping,this is the class describe map to table column
Base = declarative_base() #connect session to active the action
Session = sessionmaker(bind=engine)
session = Session() class Person(Base):
__tablename__ = 'Person'
Id = Column(Integer, primary_key=True,autoincrement=True)
Pname = Column(String,nullable=False,default='')
Address = Column(String,nullable=False,default='')
Age = Column(Integer,nullable=False,default=0) def __repr__(self):
return 'the info is ID %s Pname is %s Address is %s and Age is %s' % \
(self.Id, self.Pname, self.Address, self.Age) if __name__ == '__main__':
#add one
p = Person(Pname='bruce', Address='beijing', Age=22)
session.add(p)
session.commit() #query one
p_1 = session.query(Person).filter_by(Pname='bruce').first()
print p_1 #delete one
p_2 = session.query(Person).filter_by(Pname='bruce').first()
if p_2:
session.delete(p_2)
session.commit() #edit one
p_3 = session.query(Person).filter_by(Pname='bruce').first()
if p_3:
p_3.Age = 55
session.commit()
python下的orm基本操作(1)--Mysql下的CRUD简单操作(含源码DEMO)的更多相关文章
- Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码)
Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码) http://blog.csdn.net/helonsy/article/details/7207497 ...
- 原创:用python把链接指向的网页直接生成图片的http服务及网站(含源码及思想)
原创:用python把链接指向的网页直接生成图片的http服务及网站(含源码及思想) 总体思想: 希望让调用方通过 http调用传入一个需要生成图片的网页链接生成一个网页的图片并返回图片链接 ...
- MySQL安装(yum、二进制、源码)
MySQL安装(yum.二进制.源码) 目录 1.1 yum安装... 2 1.2 二进制安装-mysql-5.7.17. 3 1.2.1 准备工作... 3 1.2.2 解压.移动.授权... 3 ...
- Python 基于python实现的http接口自动化测试框架(含源码)
基于python实现的http+json协议接口自动化测试框架(含源码) by:授客 QQ:1033553122 欢迎加入软件性能测试交流 QQ群:7156436 由于篇幅问题,采用百度网 ...
- MySQL数据库企业级应用实践(多实例源码编译)
MySQL数据库企业级应用实践(多实例源码编译) 链接:https://pan.baidu.com/s/1ANGg3Kd_28BzQrA5ya17fQ 提取码:ekpy 复制这段内容后打开百度网盘手机 ...
- Python --链接MYSQL数据库与简单操作 含SSH链接
项目是软硬件结合,在缺少设备的情况,需要通过接口来模拟实现与设备的交互,其中就需要通过从数据库读取商品的ID信息 出于安全考虑 现在很多数据库都不允许通过直接访问,大多数是通过SSH SSH : 数 ...
- Winform下CefSharp的引用、配置、实例与报错排除(源码)
Winform下CefSharp的引用.配置.实例与报错排除 本文详细介绍了CefSharp在vs2013..net4.0环境下,创建Winfrom项目.引用CefSharp的方法,演示了winfro ...
- Entity Framework7 入门之全功能.NET版本下使用EF7(含源码)另附数据迁移常见错误处理
Entity Framework7 入门之全功能.NET(Console, WinForms, WPF等)使用EF7 昨天,我们介绍了EF的新特性和开发计划,如果你还不了解,请移步 Entity Fr ...
- vs2017 asp.net 网站发布问题 (发布路径下含源码文件)
使用vs2010版本,网站发布后会自动将源码发不为.dll程序集,但vs2017需要进行设置,其他版本没有试过. vs2017网站发布: 1. 2. 这里给一个你想用的名字,之后它会出现在你的程序文件 ...
随机推荐
- mycat初探
1:安装客户端 yum install mysql 2:安装服务端 yum install mysql-server 3:mycat要求不区分大小写 my.cnf(/etc/my.cnf)的[mysq ...
- DATE_FORMAT函数用法
一.在oracle中,当想把字符串为‘2011-09-20 08:30:45’的格式转化为日期格式,我们可以使用oracle提供的to_date函数. sql语句为: SELECT to_date(' ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- 小兔的棋盘(hdu2067)
小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- <[你在荒废时间的时候别人都在拼命!]>
如果我在这里退缩了,那么再也不可能前进 当人有了目标的时候,就会有拼命努力的动力. 当一个人真的掌握了一些东西的时候,才会觉得踏实,这就是所谓的内涵. 人生其实就是这样一步步走过去的.付出总有回报,回 ...
- wordpress自动批量定时发布插件 DX-auto-publish
DX-auto-publish是一款wordpress自动发布插件,方便实用. 该wordpress插件的主要功能如下: 1.能够自动批量定时发布wordpress站点的草稿文章,无需每篇文章都手动设 ...
- WebRTC is for Losers:WebRTC是输家
该文章是引述,仅代表作者Dave Michels观点 WebRTC is for Losers WebRTC technology has fallen short on many of its pr ...
- 苹果 Mac OS 下查看系统隐藏文件
Mac OS X中有很多系统隐藏的信息文件, 一般在Finder中都是看不到,也修改不了的. 但通过在"终端"中输入命令, 就可以在Finder中显示出来: defaults wr ...
- C# 選擇本機檔案並上傳
參考自:http://www.dotblogs.com.tw/puma/archive/2008/11/07/5910.aspxhttp://www.codeproject.com/Articles/ ...
- WinStore控件之Button
1 Buton入门简单应用 <StackPanel > <Button Content="按钮1" Height="80" Name=&quo ...