Python基础[sqlachemy]

# 依赖库安装
pip install mysql-python 
pip install sqlalchemy

# 数据库设计
import time
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import join, Table, MetaData, select, func, and_, Column,ForeignKey, Integer, String, Text, Binary, create_engine, Numeric
from sqlalchemy.orm import deferred, mapper, relationship,column_property, object_session, validates, sessionmaker, backref

DB_CONNECT_STRING = 'sqlite:///test.db'
Base = declarative_base()
engine = create_engine(DB_CONNECT_STRING, encoding='utf-8', echo=False, pool_recycle=20000) #, pool_size=1, max_overflow=10


class xTask(Base):
    # 扫描任务
    __tablename__ = 'xtask'
    scan_id = Column(String(50), primary_key=True) #unique=True
    scan_pro = Column(String(10))
    scan_host = Column(String(80)) 
    scan_path = Column(Text) 
    scan_cookie = Column(String(150), nullable=True) 
    scan_model = Column(Integer) # 0 poc, 1 scan all
    scan_dict = Column(Integer)  # 0 default, 1 custom
    scan_proxy = Column(String(80), nullable=True) 
    scan_plugin = Column(Text)
    # logs = relationship('xLog',backref = backref('test')) # ont to many


class xLog(Base):
    # 日志记录
    __tablename__ = 'xlog'
    id = Column(Integer, primary_key=True)
    scan_id = Column(String(50), nullable=False)
    p_name = Column(String(50), unique=True, nullable=False)
    status = Column(Integer, nullable=False)
    remark = Column(Text, nullable=True)
    ctime = Column(Integer, default=int(time.time()))
    # scan_id = Column(String(50), ForeignKey("xtask.scan_id"))



# 数据库操作
from sqlalchemy.orm import sessionmaker # 利用session来管理数据
from sqlalchemy import and_, or_

Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

# 统计个数, 表名: Xlog
sql_log_count2 = session.query(xLog).filter( and_(xLog.scan_id == scan_id, xLog.p_name == m_plugin_id) ).count()

# 查询
sql_log_count2 = session.query(xLog).filter( xLog.p_name == m_plugin_id) ).first()
sql_log_count2 = session.query(xLog).filter( xLog.p_name == m_plugin_id) ).one()

# 更新
sql_log_count2 = session.query(xLog).filter( xLog.p_name == m_plugin_id) ).one()
sql_log_count2.name = "test"
session.commit()
session.close()

# 增加
sql_r = xLog(
    p_name = m_plugin_id,
	status = 1 ,
	remark = "starting" ,
	scan_id = scan_id
)
session.add(sql_r)
session.commit()
session.close()

 

equals:

query.filter(User.name == 'leela')

not equals:

query.filter(User.name != 'leela')

LIKE:

query.filter(User.name.like('%leela%'))

IN:

query.filter(User.name.in_(['leela', 'akshay', 'santanu']))

# works with query objects too:

query.filter(User.name.in_(session.query(User.name).filter(User.name.like('%santanu%'))))

NOT IN:

query.filter(~User.name.in_(['lee', 'sonal', 'akshay']))

IS NULL:

filter(User.name == None)

IS NOT NULL:

filter(User.name != None)

AND:

from sqlalchemy import and_
filter(and_(User.name == 'leela', User.fullname == 'leela dharan'))

#or, default without and_ method comma separated list of conditions are AND

filter(User.name == 'leela', User.fullname == 'leela dharan')

# or call filter()/filter_by() multiple times

filter(User.name == 'leela').filter(User.fullname == 'leela dharan')

OR:

from sqlalchemy import or_
filter(or_(User.name == 'leela', User.name == 'akshay'))

match:

query.filter(User.name.match('leela'))

你可能感兴趣的:(Develop)