├── .gitignore ├── Create.py ├── Delete.py ├── Read.py ├── Update.py ├── database ├── database.py └── migrate.py ├── oop └── crud.py └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | database/__pycache__ 3 | oop/__pycache__ 4 | desktop.ini -------------------------------------------------------------------------------- /Create.py: -------------------------------------------------------------------------------- 1 | import oop.crud as init 2 | 3 | crud = init.CRUD() 4 | crud.create() -------------------------------------------------------------------------------- /Delete.py: -------------------------------------------------------------------------------- 1 | import oop.crud as init 2 | 3 | crud = init.CRUD() 4 | crud.delete() -------------------------------------------------------------------------------- /Read.py: -------------------------------------------------------------------------------- 1 | import oop.crud as init 2 | 3 | crud = init.CRUD() 4 | crud.read() -------------------------------------------------------------------------------- /Update.py: -------------------------------------------------------------------------------- 1 | import oop.crud as init 2 | 3 | crud = init.CRUD() 4 | crud.update() -------------------------------------------------------------------------------- /database/database.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | class Database: 4 | 5 | def __init__(self, database_name): 6 | self.__database_name = database_name 7 | 8 | def setLocalhost(self, localhost): 9 | self.__localhost = localhost 10 | 11 | def setUsername(self, username): 12 | self.__username = username 13 | 14 | def setPassword(self, password): 15 | self.__password = password 16 | 17 | def setTableName(self, table_name): 18 | self.__table_name = table_name 19 | 20 | def createDatabase(self): 21 | db = mysql.connector.connect( 22 | host = self.__localhost, 23 | user = self.__username, 24 | passwd = self.__password, 25 | ) 26 | 27 | cursor = db.cursor() 28 | 29 | cursor.execute("CREATE DATABASE IF NOT EXISTS "+self.__database_name) 30 | 31 | return "Database "+self.__database_name+" has been created" 32 | 33 | def createTable(self): 34 | self.createConnection() 35 | 36 | cursor = self.__db.cursor() 37 | 38 | cursor.execute("CREATE TABLE IF NOT EXISTS "+self.__table_name+" (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))") 39 | 40 | return "Table "+self.__table_name+" has been created" 41 | 42 | def createConnection(self): 43 | db = mysql.connector.connect( 44 | host = self.__localhost, 45 | user = self.__username, 46 | passwd = self.__password, 47 | database = self.__database_name 48 | ) 49 | 50 | self.__db = db 51 | 52 | -------------------------------------------------------------------------------- /database/migrate.py: -------------------------------------------------------------------------------- 1 | import database as db 2 | 3 | db = db.Database("python_crud_oop") 4 | db.setLocalhost("localhost") 5 | db.setUsername("root") 6 | db.setPassword("") 7 | db.createDatabase() 8 | 9 | db.setTableName("customers") 10 | db.createTable() 11 | -------------------------------------------------------------------------------- /oop/crud.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | 3 | class CRUD: 4 | 5 | def __init__(self): 6 | self.__localhost = "localhost" 7 | self.__username = "root" 8 | self.__password = "" 9 | self.__database_name = "python_crud_oop" 10 | self.__table_name = "customers" 11 | self.createConnection() 12 | 13 | def create(self): 14 | print('Enter your name:') 15 | name = input() 16 | print('Enter your address:') 17 | address = input() 18 | 19 | cursor = self.__db.cursor() 20 | 21 | val = (name, address) 22 | cursor.execute("INSERT INTO "+self.__table_name+" (name, address) VALUES (%s, %s)", val) 23 | 24 | self.__db.commit() 25 | 26 | print(cursor.rowcount, "record inserted.") 27 | 28 | def read(self): 29 | cursor = self.__db.cursor() 30 | 31 | cursor.execute("SELECT * FROM "+self.__table_name+"") 32 | 33 | myresult = cursor.fetchall() 34 | 35 | for x in myresult: 36 | print(x) 37 | 38 | def update(self): 39 | print('Search by id:') 40 | id = input() 41 | 42 | print('Edit name:') 43 | name = input() 44 | 45 | if name is "": 46 | print("Leaving name empty will update the value to empty as well.") 47 | 48 | print('Edit address:') 49 | address = input() 50 | 51 | if address is "": 52 | print("Leaving address empty will update the value to empty as well.") 53 | 54 | cursor = self.__db.cursor() 55 | 56 | cursor.execute ("UPDATE "+self.__table_name+" SET name=%s, address=%s WHERE id=%s ", (name, address, id)) 57 | 58 | self.__db.commit() 59 | 60 | print(cursor.rowcount, "record update.") 61 | 62 | def delete(self): 63 | print('Search by id to delete:') 64 | id = input() 65 | 66 | cursor = self.__db.cursor() 67 | 68 | cursor.execute ("DELETE FROM "+self.__table_name+" WHERE id = %s", (id,)) 69 | 70 | self.__db.commit() 71 | 72 | print(cursor.rowcount, "record deleted.") 73 | 74 | def createConnection(self): 75 | db = mysql.connector.connect( 76 | host = self.__localhost, 77 | user = self.__username, 78 | passwd = self.__password, 79 | database = self.__database_name 80 | ) 81 | 82 | self.__db = db 83 | 84 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Python OOP CRUD 2 | ### python v3.7.1 3 | 4 | # Operations 5 | 6 | * Create 7 | * Read 8 | * Update 9 | * Delete 10 | 11 | ### Bonus Operations 12 | 13 | * Create Database using Python 14 | * Create Table using Python 15 | 16 | 17 | # Configure 18 | open `database/migrate.py` and edit `localhost`, `username`, `password`, `database name` and `table name` 19 | 20 | # Usage 21 | ``` 22 | python database/migrate.py 23 | ``` 24 | ``` 25 | python Create.py 26 | ``` 27 | ``` 28 | python Read.py 29 | ``` 30 | ``` 31 | python Update.py 32 | ``` 33 | ``` 34 | python Delete.py 35 | ``` 36 | 37 | --------------------------------------------------------------------------------