-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbHandler.py
More file actions
72 lines (59 loc) · 2.26 KB
/
dbHandler.py
File metadata and controls
72 lines (59 loc) · 2.26 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
"""
Database helper class for the disk monitor
Copyright: (c) 2012 by Anusha Ranganathan.
"""
import sqlite3
import os
class dbHandler():
def __init__(self, database='example.db', table='dataOnDisk'):
self.table = table
self.database = database
self.conn = sqlite3.connect(self.database)
self.db = self.conn.cursor()
self.db.execute('''create table if not exists %s
(path text, name text, type text, size text, size_human text, ATime text, CTime text, MTime text)'''%self.table)
self.conn.commit()
def connect(self):
self.conn = sqlite3.connect(self.database)
def create(self, stats):
self.db.execute('''INSERT INTO %s VALUES ("%s","%s","%s","%s","%s","%s","%s","%s")'''%
(self.table, stats['path'], stats['name'], stats['type'], stats['size'], stats['size_human'], stats['ATime'], stats['CTime'], stats['MTime']))
self.conn.commit()
def delete(self, path):
self.db.execute('delete from %s where path="%s"'%(self.table, path))
self.conn.commit()
def modify(self, stats):
self.delete(stats['path'])
self.create(stats)
def getRecords(self, start=0, rows=100):
try:
start = int(start)
except:
start = 0
try:
rows = int(rows)
except:
rows = 100
self.db.execute('''SELECT count(path) FROM %s'''%self.table)
count = self.db.fetchone()[0]
if start >= count:
start = 0
self.db.execute('''SELECT * FROM %s LIMIT %d,%d'''%(self.table, start, rows))
header = self.db.description
header = tuple([x[0] for x in header])
answer = self.db.fetchall()
answer.insert(0, header)
return answer
def getRecord(self, path):
self.db.execute('''SELECT * FROM %s WHERE path="%s"'''%(self.table, path))
header = self.db.description
header = tuple([x[0] for x in header])
answer = self.db.fetchall()
ans = {}
if len(answer) == 1:
for i in range(len(header)):
ans[header[i]] = answer[0][i]
return ans
def close(self):
self.conn.close()