Check in first version of dbtools
This commit is contained in:
parent
77e66918db
commit
e0abdf0a56
1 changed files with 64 additions and 0 deletions
64
dbtools/__init__.py
Normal file
64
dbtools/__init__.py
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
import sqlite3, pymysql, pymysql.cursors
|
||||||
|
|
||||||
|
SQLITE = 0
|
||||||
|
MYSQL = 1
|
||||||
|
MARIADB = MYSQL
|
||||||
|
|
||||||
|
class dbObject:
|
||||||
|
|
||||||
|
# --------------------------------------------- Initialization -------------------------------------------------
|
||||||
|
|
||||||
|
def initMySQL(self, host, port, user, pwd, db):
|
||||||
|
self.conn = pymysql.connect(host = host, port = port, user = user, password = pwd, db = db, charset = "utf8mb4", cursorclass = pymysql.cursors.DictCursor)
|
||||||
|
self.cur = conn.cursor()
|
||||||
|
self.dbtype = MYSQL
|
||||||
|
self.host = host
|
||||||
|
self.port = port
|
||||||
|
self.user = user
|
||||||
|
self.pwd = pwd
|
||||||
|
self.db = db
|
||||||
|
|
||||||
|
def initSQLite(self, path):
|
||||||
|
self.conn = sqlite3.connect(path)
|
||||||
|
self.cur = self.conn.cursor()
|
||||||
|
self.dbtype = SQLITE
|
||||||
|
self.path = path
|
||||||
|
|
||||||
|
def __init__(self, dbtype = SQLITE, path = 'Database.db', host = None, port = None, user = None, pwd = None, db = None):
|
||||||
|
|
||||||
|
if dbtype == SQLITE:
|
||||||
|
self.initSQLite(path)
|
||||||
|
|
||||||
|
elif dbtype == MYSQL:
|
||||||
|
self.initMySQL(host, port or 3306, user, pwd, db)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise ValueError("Unknown database type %s." % str(dbtype))
|
||||||
|
|
||||||
|
# ---------------------------------------------- No more initialization ----------------------------------------
|
||||||
|
|
||||||
|
def closeConnection(self):
|
||||||
|
return self.conn.close()
|
||||||
|
|
||||||
|
def commit(self):
|
||||||
|
return self.conn.commit()
|
||||||
|
|
||||||
|
def executeQuery(self, query):
|
||||||
|
return self.cur.execute(query)
|
||||||
|
|
||||||
|
def getNext(self):
|
||||||
|
return self.cur.fetchone()
|
||||||
|
|
||||||
|
def isInitialized(self):
|
||||||
|
try:
|
||||||
|
self.executeQuery("SELECT * FROM tweets")
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def getFLDate(self, val = 0):
|
||||||
|
if val == 0:
|
||||||
|
mode = "MIN"
|
||||||
|
else:
|
||||||
|
mode = "MAX"
|
||||||
|
return getDate(str(list(self.executeQuery("SELECT %s(SUBSTR(timestamp,0,11)) FROM tweets" % mode))[0][0]))
|
Loading…
Reference in a new issue