developer_analysis/chemical.py

45 lines
1.0 KiB
Python
Raw Normal View History

2024-10-13 10:51:07 +00:00
import sqlite3
from uuid import uuid4
class Chemical :
def __init__ (self, conn) :
self.conn = conn
self.cursor = conn.cursor()
def uuid (self) :
return str(uuid4())
def get (self, name) :
query = "SELECT chemical_id FROM chemicals WHERE (name = LOWER(?)) LIMIT 1;"
res = self.cursor.execute(query, (name,))
return self.cursor.fetchone()
def exists (self, name) :
chemical = self.get(name)
if chemical is not None:
return chemical[0]
return ''
def create (self, name) :
id = self.uuid()
query = "INSERT OR IGNORE INTO chemicals (chemical_id, name) VALUES (?, LOWER(?));"
self.cursor.execute(query, (id, name,))
self.conn.commit()
return id
def identify (self, c) :
chemical_id = self.exists(c)
if chemical_id == '' :
chemical_id = self.create(c)
return chemical_id
def table (self) :
query = "SELECT * FROM chemicals;"
res = self.cursor.execute(query, ())
for row in self.cursor.fetchall() :
print(row)
if __name__ == '__main__' :
con = sqlite3.connect('developers.sqlite')
c = Chemical(con)
c.table()