Created
May 28, 2021 15:30
-
-
Save Midnex/4dac90d943afab0f58844bfaed33c788 to your computer and use it in GitHub Desktop.
u/BRTXFF/nmhlsm
This file contains hidden or 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
# https://www.reddit.com/r/learnpython/comments/nmhlsm/reddit_post/ | |
# try this. | |
import psycopg2 | |
class Database: | |
def __init__(self, db): | |
connection = psycopg2.connect(host="localhost", database="test", user="postgres", password="password") | |
self.cur = self.conn.cursor() | |
self.cur.execute("CREATE TABLE IF NOT EXISTS parts (id INTEGER PRIMARY KEY, part text,customer text, retailer text, price text)") | |
self.conn.commit() | |
def fetch(self): | |
self.cur.execute("SELECT * FROM parts") | |
rows = self.cur.fetchall() | |
return rows | |
def insert(self, part, customer, retailer, price): | |
self.cur.execute("INSERT INTO parts VALUES (NULL, ?, ?, ?, ?)", (part, customer, retailer, price)) | |
self.conn.commit() | |
def remove(self, id): | |
self.cur.execute("DELETE FROM parts WHERE id=?",(id,)) | |
self.conn.commit() | |
def update(self, id, part, customer, retailer, price): | |
self.cur.execute("UPDATE parts SET part = ?, customer = ?, retailer = ?, price= ? WHERE id = ?", (part, customer, retailer, price, id)) | |
self.conn.commit() | |
def __del__(self): | |
self.conn.close() | |
db = Database('store.db') | |
db.insert("4GB DDR4 Ram", "John Doe", "Microcenter", "160") | |
db.insert("Asus Mobo", "Mike Henry", "Microcenter", "360") | |
db.insert("Asus Mobo", "Mike Henry", "Microcenter", "360") | |
db.insert("500w PSU", "Karen Johnson", "Newegg", "80") | |
db.insert("2GB DDR4 Ram", "Karen Johnson", "Newegg", "70") | |
db.insert("24 inch Samsung Monitor", "Sam Smith", "Best Buy", "180") | |
db.insert("NVIDIA RTX 2080", "Albert Kingston", "Newegg", "679") | |
db.insert("600w Corsair PSU", "Karen Johnson", "Newegg", "130") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment