我有这两个存储不同数据的数据库。我正在通过python创建数据库,如下所示:
db = SQLAlchemy(app)
# Many-to-Many Association table
visits = db.Table('Visits',
db.Column('clients', db.Integer, db.ForeignKey('ClientIPAddresses.id')),
db.Column('domains_visited', db.Integer, db.ForeignKey('Domains.id'))
)
'''This Class Creates a Table in the Database which stores unique ip addresses'''
class ClientIPAddresses(db.Model):
__tablename__ = 'ClientIPAddresses'
id = db.Column(db.Integer, primary_key=True)
IPAddress = db.Column(db.String(80), unique=True)
Clients = db.relationship('Domains', secondary=visits, backref=db.backref('ClientIPs', lazy='dynamic'))
def __init__(self, IPAddress):
self.IPAddress = IPAddress
def __repr__(self):
return '<Name %r>' % self.name
'''This Class Creates a Table in the Database which stores date, ip and domains visited.'''
class Domains(db.Model):
__tablename__ = 'Domains'
id = db.Column(db.Integer, primary_key=True)
DateandTime = db.Column(db.String(80), unique=False)
IPAddress = db.Column(db.String(80), unique=False)
DomainsVisited = db.Column(db.String(80), unique=False)
def __init__(self, DateandTime, IPAddress, DomainsVisited):
self.DateandTime = DateandTime
self.IPAddress = IPAddress
self.DomainsVisited = DomainsVisited
def __repr__(self):
return '<Name %r>' % self.name
此外,我还创建了一个多对多关联表。(但是我没有添加任何内容)插入值后的数据库如下所示:
ClientIPAddresses Table:
id | IPAddress
----+---------------
1 | 192.168.1.24
2 | 192.168.1.31
3 | 192.168.1.118
4 | 192.168.1.136
5 | 192.168.1.17
Domains Table:
id | DateandTime | IPAddress | DomainsVisited
----+-----------------+---------------+-------------------------------------
1 | Jun 15 16:25:39 | 192.168.1.24 | armdl.adobe.com
2 | Jun 15 16:25:39 | 192.168.1.24 | play.google.com
3 | Jun 15 16:25:39 | 192.168.1.31 | disorderstatus.ru
4 | Jun 15 16:25:39 | 192.168.1.31 | impressions.pezasys.net
5 | Jun 15 16:25:40 | 192.168.1.31 | differentia.ru
6 | Jun 15 16:25:42 | 192.168.1.17 | play.google.com
7 | Jun 15 16:25:42 | 192.168.1.24 | armdl.adobe.com
8 | Jun 15 16:25:42 | 192.168.1.118 | impressions.pezasys.net
9 | Jun 15 16:25:42 | 192.168.1.118 | differentia.ru
10 | Jun 15 16:25:43 | 192.168.1.118 | armdl.adobe.com
我要以这种形式显示输出:
Output:
sl.| IP | domains
----+-----------------+---------------+-------------------------------------
1 | 192.168.1.118 | armdl.adobe.com
| | differentia.ru
| | impressions.pezasys.net
2 | 192.168.1.17 | play.google.com
3 | 192.168.1.24 | play.google.com
| | armdl.adobe.com
4 | 192.168.1.31 | differentia.ru
| | impressions.pezasys.net
| | disorderstatus.ru
即我想为一个唯一的IP显示访问的不同域是什么,如何显示相同的域?谢谢。