Merge pull request #742 from pi-hole/dschaper-gravityDB

gravityDB
This commit is contained in:
Adam Warner 2016-10-02 11:28:47 +01:00 committed by GitHub
commit 14fc90579d

View file

@ -43,52 +43,33 @@
import sqlite3 import sqlite3
#----------------------------------------------------------------------------- # Logfile containing unique list of all domains on downloaded lists.
# Functions logfile = '/etc/pihole/pihole.2.eventHorizon.txt'
#-----------------------------------------------------------------------------
def create_tables():
qt = 'DROP TABLE IF EXISTS gravity' # Create the SQLite connection
c.execute(qt) conn = sqlite3.connect('/etc/pihole/pihole.db')
conn.commit()
# Python auto-handle commits, no need to call for commits manually
with conn:
c = conn.cursor()
# Lists have just been downloaded, clear out the existing data
c.execute('DROP TABLE IF EXISTS gravity')
# Ready new table for list of domains
qt = ''' qt = '''
CREATE TABLE IF NOT EXISTS gravity ( CREATE TABLE IF NOT EXISTS gravity (
idx integer primary key autoincrement, idx INTEGER PRIMARY KEY ASC,
domain text domain text
) )
''' '''
c.execute(qt) c.execute(qt)
conn.commit()
#----------------------------------------------------------------------------- # enable WAL mode
# Main c.execute('PRAGMA journal_mode=WAL;')
#-----------------------------------------------------------------------------
logfile = '/etc/pihole/pihole.2.eventHorizon.txt'
counts = {'lc': 0}
# Create the SQLite connection
conn = sqlite3.connect('/etc/pihole/pihole.db')
conn.text_factory = str # I don't like it as a fix, but it works for now
c = conn.cursor()
create_tables()
sql = "DELETE FROM gravity"
c.execute(sql)
conn.commit()
# Parse the log file.
for line in open(logfile):
line = line.rstrip()
counts['lc'] += 1
if (counts['lc'] % 10000) == 0:
conn.commit()
# Parse the log file into the database
with open(logfile) as f:
for line in f:
sql = "INSERT INTO gravity (domain) VALUES (?)" sql = "INSERT INTO gravity (domain) VALUES (?)"
c.execute(sql, (line,)) c.execute(sql, (line,))
conn.commit()