import sqlite3
class SQLiteConnection(object):
def __init__(self, db):
self.DB_PATH = db
self.DB = None
self.CURSOR = None
self.STMT = None
self.SQL = None
self.PARAMS = None
self.RESULTS = None
def connection(self):
self.DB = sqlite3.connect(self.DB_PATH)
self.CURSOR = self.DB.cursor()
def desconnection(self):
self.DB.close()
def prepareSQL(self):
if self.PARAMS != None:
self.STMT = self.SQL % (self.PARAMS)
else:
self.STMT = self.SQL
def getData(self):
self.RESULTS = self.CURSOR.fetchall()
def resetAttributes(self):
self.DB = None
self.CURSOR = None
self.STMT = None
self.SQL = None
self.PARAMS = None
self.RESULTS = None
def getResults(self):
return self.RESULTS
self.resetAttributes()
def executeSQL(self, sql, params=None, fields=None):
self.SQL = sql
self.PARAMS = params
self.connection()
self.prepareSQL()
self.CURSOR.execute(self.STMT)
if fields:
self.getData()
else:
if "INSERT" in self.SQL:
self.DB.commit()
self.RESULTS = self.CURSOR.lastrowid
elif "UPDATE" in self.SQL:
self.DB.commit()
elif "DELETE" in self.SQL:
self.DB.commit()
self.desconnection()
# Example
# Get All Rows
DB = SQLiteConnection("path/to/your/sqlite.db")
args = "data"
DB.executeSQL("SELECT * FROM table WHERE column = %s", args, True)
rows = DB.getResults()
# ......
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.