最近逐漸打算將工做的環境轉移到ubuntu下,忽然發現對於我來講,這ubuntu對於我這種上上網,收收郵件,寫寫博客,寫寫程序的時實在是太合適了,除了剛接觸的時候會不怎麼徹底適應命令行及各類權限管理,apt-get命令至關的方便,各類原先在windows下各類奇怪錯誤在ubuntu下都沒有出現了,好了,我就不說廢話了,今天大體簡單的介紹下python下的ORM to Mysql 的操做(注意:必定要看官網的文檔!)html
refer:http://docs.sqlalchemy.org/en/latest/orm/tutorial.htmlpython
一,準備環境mysql
1.安裝mysql-server (在此以前請準備好Python的環境)sql
2.安裝mysql-python 這裏有點坑,我直接使用apt-get命令沒有成功,後來使用pip安裝成功的數據庫
~$ pip install mysql-python
3.安裝sqlalchemy ubuntu
準備環境OK以後,安裝sqlalchemy windows
~$ pip install sqlalchemy
若是你在第二步 pip install mysql-python 如圖的相似的問題,這是須要安裝connector for c 一些環境,若是你是x64的環境,請選中裏面的x86,x64,都要安裝bash
下載列表:http://dev.mysql.com/downloads/connector/c/6.0.html#downloadssession
參考的解決方案:http://stackoverflow.com/questions/1972259/cannot-open-include-file-config-win-h-no-such-file-or-directory-while-instapp
環境都準備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()