Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
gpfs-scan-files/DB.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
51 lines (50 sloc)
1.17 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Provide stateful DB access accessible across functions | |
import sqlite3 | |
class DB: | |
def __init__(self,dbpath): | |
self.db = sqlite3.connect(dbpath) | |
self.dbc = self.db.cursor() | |
def write(self,sqls): | |
# If sqls is a single string, convert to list of one | |
if not type(sqls) in ( type(()), type([]) ): sqls = [ sqls ] | |
for sql in sqls: | |
self.dbc.execute(sql) | |
self.db.commit() | |
def read(self,sql): | |
self.dbc.execute(sql) | |
return self.dbc.fetchall() | |
def close(self): | |
self.db.close() | |
# Only needed when creating the database | |
def create_tables(self): | |
CREATE_TABLE_SQLS = [ | |
"""CREATE TABLE IF NOT EXISTS user_footprint ( | |
datetime text, | |
userid integer, | |
user text, | |
rank integer, | |
count integer, | |
footprint integer, | |
primary key (datetime,user) | |
); | |
""", | |
"""CREATE TABLE IF NOT EXISTS homedir_footprint ( | |
datetime text, | |
dir text, | |
rank integer, | |
count integer, | |
footprint integer, | |
primary key (datetime,dir) | |
); | |
""", | |
"""CREATE TABLE IF NOT EXISTS shareddir_footprint ( | |
datetime text, | |
dir text, | |
rank integer, | |
count integer, | |
footprint integer, | |
primary key (datetime,dir) | |
); | |
""" | |
] | |
self.write(CREATE_TABLE_SQLS) |