Skip to content

Commit

Permalink
Create database.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored May 10, 2024
1 parent d94390d commit 2f1562d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions database/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sqlite3

class Database:
def __init__(self):
self.connection = sqlite3.connect('banking.db')
self.cursor = self.connection.cursor()

def create_table(self):
self.cursor.execute('''CREATE TABLE IF NOT EXISTS accounts (id INTEGER PRIMARY KEY, name TEXT, balance REAL)''')
self.connection.commit()

def insert_account(self, name, balance):
self.cursor.execute("INSERT INTO accounts (name, balance) VALUES (?, ?)", (name, balance))
self.connection.commit()

def update_account(self, id, name, balance):
self.cursor.execute("UPDATE accounts SET name=?, balance=? WHERE id=?", (name, balance, id))
self.connection.commit()

def delete_account(self, id):
self.cursor.execute("DELETE FROM accounts WHERE id=?", (id,))
self.connection.commit()

def get_account(self, id):
self.cursor.execute("SELECT * FROM accounts WHERE id=?", (id,))
return self.cursor.fetchone()

def get_all_accounts(self):
self.cursor.execute("SELECT * FROM accounts")
return self.cursor.fetchall()

0 comments on commit 2f1562d

Please sign in to comment.