forked from wangxiaomo/SmartWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.py
34 lines (28 loc) · 964 Bytes
/
Helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#-*- coding: utf-8 -*-
import re
import config
from lib.SQLite import SQLite
def log(mesg):
print mesg
def sql_escape(string):
return string.replace('\\', '\\\\').replace('\'', '\'\'')
def is_message_exists(message):
sql = "SELECT * FROM sw_messages WHERE src='%s' AND dst='%s' AND message='%s' AND time='%s'" % \
(message["src"], message["dst"], message["message"], message["time"])
db = SQLite(config.DB_FILE)
rows = db.fetch_sql(sql)
if rows:
return True
else:
return False
def save_2_sqlite(messages):
db = SQLite(config.DB_FILE)
count = 0
for message in messages:
if is_message_exists(message):
count += 1
continue
sql = "INSERT INTO sw_messages(src,dst,message,time) VALUES('%s', '%s', '%s', '%s')" % \
(message["src"], message["dst"], message["message"], message["time"])
db.do_sql(sql)
log("%d messages existed!" % count)