最近逐漸打算將工作的環境轉移到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()