系统centos
refer:http://docs.sqlalchemy.org/en/latest/orm/tutorial.html
〇,先讲自己碰到的问题
1,安装了mysql-server,python,等环境
2.很重要的一个问题:我的mysql没有安装mysql-devel!!!!!!!!这导致了后面若干个问题!!!!这个是mysql的包管理工具。
2.安装mysql-python:
这个是我手动安装的,开始yum安装因为mysql-devel没有安装一直有问题。手动安装步骤
1.下载tar.gz包:用gunzip解压出tar包,然后用tar xvf解压。
2.然后进入目录,python2 setup.py build python2 setup.py install就可以了。
3.安装sqlalchemy也是手动下载包安装的。
4.下面就是安装此文中代码部分实现的了。
一,准备环境
1.安装mysql-server (在此之前请准备好Python的环境)
2.安装mysql-python
centos:yum install sqlalchemy -y
3.安装sqlalchemy
准备环境OK之后,安装sqlalchemy
总是出现No module named MySQLdb这个错误:后来通过错误提示信息查到是因为:mysql少安装了(yum install mysql-devel)这个是mysql的管理安装包。
参考的解决方案: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:[email protected]: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:[email protected]: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()