Python:數據庫操作模塊SQLAlchemy
阿新 • • 發佈:2017-10-09
conf 一個數 1.3 查看 == tip for 修改 1.4 SQLAlchemy的ORM是一個映射函數(Mapper),將Python中定義的類與數據庫中的表建立關聯,以及類的實例(instance)和表的行(row)建立關聯。
查看一個類所對應的數據庫表,使用__tablename__屬性,例如 User.__tablename__
1. 查詢數據 (query)
1.1 查詢一個trace中flow個數(to count flows of specific trace)
session.query(Flow).filter(Flow.trace_id == 1).count()
from sqlalchemy import distinct
from config import *
session = DBSession()
session.query(Flow.srcIP).filter(Flow.trace_id == 1).distinct().count()
1.3 查詢一個trace中不同的dstIP和dstPort對的個數(to count distinct dstIP and dstPort)
session.query(Flow.dstIP, Flow.dstPort).filter(Flow.trace_id == 1).distinct().count()
1.4 查詢指定列的數據,返回一個KeyedTuple數據類型的列表( get a tuple list of specified columns )
n = session.query(Flow.dstIP, Flow.dstPort).filter(Flow.trace_id == 1).all()
# The type of n is list.
# The type of n[0] is sqlalchemy.util._collections.KeyedTuple
1.5 查詢指定列中的所有不同值( get a distinct tuple list of specified columns)
n = session.query(Flow.dstIP, Flow.dstPort).filter(Flow.trace_id == 1).distinct() .all()
1.6 獲得一列數據的平均值(get average value of a column)
# sql language: select avg(txPkt) from Flow
from sqlalchemy.sql import func
q = session.query(func.avg(Flow.txPkt)).filter(Flow.trace_id == 1)
print q[0][0]
# The type of q is sqlalchemy.orm.query.Query
# The type of q[0] is sqlalchemy.util._collections.KeyedTuple
# The type of q[0][0] is decimal.Decimal
1.7 多列數據平均值的計算(compute average values of columns)
q = session.query((func.avg(Flow.txPkt)+func.avg(Flow.rxPkt))/2).filter(Flow.trace_id == 1)
1.8 對查詢到的數據排序(order by )
from sqlalchemy import desc
q = session.query(Flow.timestamp).filter(trace_id == 1).order_by(desc(Flow.timestamp))
1.9 分組查詢
q = session.query(Flow.dstIP, Flow.dstPort, func.count(Flow.id)).filter(Flow.trace_id == tid).group_by(Flow.dstIP, Flow.dstPort).all()
2 查詢中,常用的過濾操作
等於(equals), 例如 query.filter(name == ‘Jack‘)
不等於(not equals), 例如 query.filter(name != ‘Jack‘)
在列表中(in), 例如 query.filter(name.in_([‘Micheal‘, ‘Bob‘, ‘Jack‘]))
不在列表中(not in), 例如query.filter(~name.in_([‘Micheal‘, ‘Bob‘, ‘Jack‘]))
空值(null), 例如 query.filter(name == None)
不是空值(not null), 例如 query.filter(name != None)
與(and), 例如 query.filter(and_(name == ‘Andy‘, fullname == ‘Andy Liu‘ ))
and_可以省略, 例如 query.filter(name==‘Andy‘, fullname==‘Andy Liu‘)
或(or), 例如 query.filter(or_(name == ‘Andy‘, name == ‘Micheal‘))
2. 表的數據操作(table data operation)
2.1 添加\刪除一個column ( add a new column to a table)
from db import engine
from sqlalchemy import DDL
add_column = DDL(‘alter table Flow add column cluster_id integer after trace_id‘)
drop_column = DDL(‘alter table Flow drop column microsecond‘)
engine.execute(add_column)
engine.execute(drop_column)
2.2 修改一個數據(update a value)
session.query(Flow).filter(Flow.dstIP == dstIP, Flow.dstPort == dstPort, Flow.trace_id == 1).update({‘cluster_id‘ : 0})
2.3 插入一行數據(insert a row)
session = DBSession()
cluster = Clusters(trace_id = tid, cluster_id = cid, \
dstIP = dIP, dstPort = dPort, \
avgPkt = aPkt, avgByte = aByte, \
size = count)
session.add(cluster)
session.commit() # commit or flush
session.close()
2.4 刪除一行數據(delete a row )
session = DBSession()
session.query(Clusters).filter(Clusters.trace_id = 2).delete()
session.commit() # commit or flush
session.close()
補充:
外鍵 ForeignKey只能引用外表的指定列中已經存在的值。
1.2. 查詢一個trace中不同srcIP的個數 (to count distinct srcIP)
Python:數據庫操作模塊SQLAlchemy