dp.db package

Submodules

dp.db.mongo module

File: mongo.py Desc: mongodb操作类 Author:yanjingang(yanjingang@mail.com) Date: 2019/2/21 23:34

class dp.db.mongo.MongoDB(dbconf)

Bases: object

mongodb操作基础类

aggregate(pipeline, name=None)

mongo中聚合操作

Parameters:
  • pipeline – 聚合管道操作列表
  • name – 数据库表名
Returns:

connect()

连接mongodb

Returns:
count(cond=None, name=None)

查询mongo符合查询条件的记录数

Parameters:
  • cond – 查询条件
  • name – 表名
Returns:

符合条件的记录数

disconnect()

断开mongodb连接

Returns:
find(cond={}, fields='', sort=None, limit=0, name=None, skip=0, no_cursor_timeout=False, batch_size=None)

查询

Parameters:
  • cond – 查询条件
  • fields – 查询字段,多个字段之间,分隔
  • sort – 排序条件,按哪个字段进行排序
  • limit – 限制返回条数
  • name – 数据表名
  • skip – 跳过的记录数
  • no_cursor_timeout – 默认为False,会在10分钟之后关闭,如果10分钟处理不完,可以设置为True,但是需自己调用close()显示关闭游标
  • batch_size – 默认为None,表示使用默认的返回条数/容量限制,用户可以设置条数,比如30,对于过大值,pymongo内部会使用默认配置
Returns:

find_one(cond={}, fields='', name=None)

查询,返回单条数据

Parameters:
  • cond – 查询条件
  • fields – 查询字段,多个字段之间,分隔
  • name – 数据表名
Returns:

get_collection(name)

返回表collection对象

Parameters:name – 集合名称(即表名)
Returns:集合对象
insert(data, name=None)

将数据插入到mongo中

Parameters:
  • name – 表名
  • cond – 修改条件
  • data – 数据
Returns:

remove(cond, name=None)

删除符合某条件的数据

Parameters:
  • name – 表名
  • cond – 条件
Returns:

update(cond, data, name=None, isset=True, upsert=False, multi=False, attempts=1)

将数据更新到mongo中,如果不存在则插入

Parameters:
  • cond – 修改条件
  • data – 需要插入或修改的数据
  • isset – 是否使用set
  • upsert – 不存在时是否插入
  • name – 表名
  • multi – 是否修改多个
  • attempts – 尝试更新的次数,默认只更新一次, 最多执行三次
Returns:

dp.db.mysql module

File: mysql.py Desc: mysql操作类

class dp.db.mysql.Mysql(host, port, user, password, db, charset='utf8', debug=0)

Bases: object

Mysql操作类

connect()
count(table, where=None)

查询记录条数

Parameters:
  • table – 表名
  • where – where条件键值对
Returns:

记录条数

debug(sql)
delete(table, where)

删除数据

Parameters:
  • table – 表名
  • where – where条件键值对
Returns:

disconnect()
execute(sql)

复杂sql执行(慎用-注意性能)

Parameters:sql – 特殊sql语句
Returns:
insert(table, values)

插入数据

Parameters:
  • table – 表名
  • values – dict键值对
Returns:

数据id

query(table, where=None, select='*', groupby='', orderby='', limit=1000)

查询数据

Parameters:
  • table – 表名
  • where – where条件键值对
  • select – 查询字段
  • groupby – 分组
  • orderby – 排序
  • limit – 限定返回条数(默认1000)
Returns:

结果集list

update(table, where, values)

更新数据

Parameters:
  • table – 表名
  • where – where条件键值对
  • values – 要更新的键值对
Returns:

dp.db.mysqlgenerator module

File: mysqlgenerator.py Desc: mysql sql语句生成

class dp.db.mysqlgenerator.MySqlGenerator

Bases: object

sqlgenerator

static delete(table, where={})
static format_set(k, v)

返回k/v对应的set语句

static format_where(k, v)

返回k/v对应的where语句

static insert(table, values={})
static query(table, select=None, where={}, groupby=None, orderby=None, limit=None)
static update(table, where, values={})

Module contents