1. SQLAlchemy
Python2/3
都支持它,SQLAlchemy 自己无法操作数据库,必须结合 pymsql 等第三方插件,所以也需要安装pymysql
。
pip2 install pymysql
pip2 install SQLAlchemy
用法:
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.orm import declarative_base, Session
# 声明ORM的一个基类并建立映射关系
Base = declarative_base()
class PositionRegister(Base):
__tablename__ = 'statistic_fc_position_register'
id = Column(Integer, name='id', primary_key=True)
date = Column(DateTime, name='date')
position_id = Column(Integer, name='position_id')
position_name = Column(String(32), name='position_name', nullable=True)
register_count = Column(Integer, name='register_count')
engine = create_engine("mysql+pymysql://%s:%s@%s:3306/%s" % (config.get('mysqluser')
, config.get('mysqlpassword')
, config.get('mysqlhost')
, config.get('mysqldbname')), max_overflow=5)
session = Session(bind=engine, future=True)
position_regis = PositionRegister()
position_regis.date = day
position_regis.position_id = position_id
position_regis.position_name = position_name
position_regis.register_count = register_count
session.add(position_regis)
session.commit()
session.close()
2. orator
python2不支持orator,python3可以使用这种:
pip3 install orator
文章评论