53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
import sqlite3
|
|
from uuid import uuid4
|
|
|
|
class Supply :
|
|
def __init__ (self, conn) :
|
|
self.conn = conn
|
|
self.cursor = conn.cursor()
|
|
|
|
def uuid (self) :
|
|
return str(uuid4())
|
|
|
|
def get (self, name) :
|
|
query = "SELECT supply_id FROM supply WHERE (url = ?) LIMIT 1;"
|
|
res = self.cursor.execute(query, (name,))
|
|
return self.cursor.fetchone()
|
|
|
|
def exists (self, name) :
|
|
supply = self.get(name)
|
|
if supply is not None:
|
|
return supply[0]
|
|
return ''
|
|
|
|
def create (self, chemical_id, url, g, ml, price) :
|
|
id = self.uuid()
|
|
query = "INSERT OR IGNORE INTO supply (supply_id,chemical_id,url,grams,milliliters,price) VALUES (?,?,?,?,?,?);"
|
|
self.cursor.execute(query, (id,chemical_id,url,g,ml,price,))
|
|
self.conn.commit()
|
|
return id
|
|
|
|
def identify (self, chemical_id, url, g, ml, price) :
|
|
supply_id = self.exists(url)
|
|
if supply_id == '' :
|
|
supply_id = self.create(chemical_id, url, g, ml, price)
|
|
return supply_id
|
|
|
|
def table (self) :
|
|
query = "SELECT * FROM supply;"
|
|
res = self.cursor.execute(query, ())
|
|
for row in self.cursor.fetchall() :
|
|
print(row)
|
|
|
|
def display (self, supply_id, chemical) :
|
|
query="SELECT round((price/100.0)/grams, 2) as ppg FROM supply WHERE (supply_id = ?);" # ORDER BY ppg ASC
|
|
res = self.cursor.execute(query, (supply_id,))
|
|
data = self.cursor.fetchone()
|
|
ratio = data[0]
|
|
print(f'{chemical}: {ratio} $/g')
|
|
|
|
if __name__ == '__main__' :
|
|
con = sqlite3.connect('developers.sqlite')
|
|
s = Supply(con)
|
|
s.table()
|