From dcece5c237f7fb009bb8747e13dca5c798bfe048 Mon Sep 17 00:00:00 2001 From: Bhargav Kuvadiya Date: Sat, 1 Oct 2022 13:03:29 +0530 Subject: [PATCH] Added Database-As-Storage & Updated Main README.md --- Database-As-Storage/Database-As-Storage.py | 212 +++++++++++++++++++++ Database-As-Storage/README.md | 96 ++++++++++ Database-As-Storage/requirements.txt | 0 README.md | 25 +-- 4 files changed, 321 insertions(+), 12 deletions(-) create mode 100644 Database-As-Storage/Database-As-Storage.py create mode 100644 Database-As-Storage/README.md create mode 100644 Database-As-Storage/requirements.txt diff --git a/Database-As-Storage/Database-As-Storage.py b/Database-As-Storage/Database-As-Storage.py new file mode 100644 index 0000000..8aba837 --- /dev/null +++ b/Database-As-Storage/Database-As-Storage.py @@ -0,0 +1,212 @@ +from dataclasses import field, fields +import sqlite3 +from unittest import result + +class CustomSqliteAction(): + def __init__(self,database_name, database_table, database_fields): + self.database = database_name + self.table_name = database_table + self.fields = database_fields + + self.db_conn = None + self.valid_fields = None + + def connect_to_db(self): + print('[+] Connecting to {}'.format(self.database)) + db_conn = sqlite3.connect(self.database) + self.db_conn = db_conn + + def load_table_info(self): + self.valid_fields = [f_name[0] for f_name in self.fields] + + def table_validation(self,inserted_fields): + return list(set(inserted_fields).difference(set(self.valid_fields))) + + def _parse_result(self,db_data): + query_set = [] + for data in db_data: + data_dict = {k: v for k, v in zip(self.valid_fields, data)} + query_set.append(data_dict) + return query_set + + def create_table(self): + sql_string = """CREATE TABLE IF NOT EXISTS {table_name} {field_string};""" + field_string = "("+", ".join([" ".join(fi for fi in f) for f in self.fields])+")" + sql_string = sql_string.format(table_name=self.table_name,field_string=field_string) + + print("[+] Creating Table {} .....\n".format(self.table_name)) + cur = self.db_conn.cursor() + cur.execute(sql_string) + + def table_exists(self): + sql_string = """SELECT * FROM {}""".format(self.table_name) + try: + cur = self.db_conn.cursor() + cur.execute(sql_string) + print('[+] Connecting To Table {}\n'.format(self.table_name)) + return True + except sqlite3.OperationalError: + print('[-] TABLE NAME {} DOES NOT EXISTS'.format(self.table_name)) + return False + + def store_data(self,**kwargs): + validation = self.table_validation(kwargs.keys()) + if not validation: + sql_string = """INSERT INTO {table_name} {field_string} VALUES {value_string};""" + field_string = "("+", ".join([f for f in kwargs.keys()])+")" + value_string = "("+ ", ".join([f"'{v}'" for v in kwargs.values()]) +")" + + sql_string = sql_string.format(table_name=self.table_name,field_string=field_string,value_string=value_string) + cur = self.db_conn.cursor() + try: + cur.execute(sql_string) + except sqlite3.OperationalError: + print('[-] Database Syntax Error probabily because of \' in data ') + self.db_conn.commit() + else: + print('\n[-] STORE DATA ERROR ...') + print('[-] {} IS NOT VALID FIELD NAME'.format(', '.join(validation))) + print('[-] CHOICES ARE {}'.format((', ').join(self.valid_fields))) + + def delete_data(self,**kwargs): + validation = self.table_validation(kwargs.keys()) + if not validation: + if len(kwargs) == 1: + sql_string = """DELETE FROM {table_name} WHERE {field} = '{field_id}';""" + sql_string = sql_string.format(table_name=self.table_name,field=list(kwargs.keys())[0],field_id=list(kwargs.values())[0]) + elif len(kwargs) > 1: + inintial_string = """DELETE FROM {table_name} WHERE """.format(table_name=self.table_name) + field_string = " AND ".join(["{field} = '{field_value}'".format(field=f[0],field_value=f[1]) for f in kwargs.items() ]) + ";" + sql_string = inintial_string + field_string + else: + print('[-] At least Provide 1 Argument') + return + + cur = self.db_conn.cursor() + cur.execute(sql_string) + self.db_conn.commit() + print("[+] Delete Data Successfully") + + else: + print('\n[-] DELETE DATA ERROR ...') + print('[-] {} IS NOT VALID FIELD NAME'.format(', '.join(validation))) + print('[-] CHOICES ARE {}'.format((', ').join(self.valid_fields))) + + def update_data(self,search_tuple, **kwargs): + validation = self.table_validation(kwargs.keys()) + if not validation: + if len(kwargs) == 1: + sql_string = """UPDATE {table_name} SET {field} = '{update_value}' WHERE {p_field} = {field_id};""" + sql_string = sql_string.format(table_name=self.table_name, field=list(kwargs.keys())[0], update_value=list(kwargs.values())[0], p_field=search_tuple[0], field_id=search_tuple[1]) + else: + print('[-] Only One Upadte Argument Allowed') + return + cur = self.db_conn.cursor() + cur.execute(sql_string) + self.db_conn.commit() + print("[+] Update Data Successfully") + else: + print('\n[-] DELETE DATA ERROR ...') + print('[-] {} IS NOT VALID FIELD NAME'.format(', '.join(validation))) + print('[-] CHOICES ARE {}'.format((', ').join(self.valid_fields))) + + def read_data(self,**kwargs): + validation = self.table_validation(kwargs.keys()) + if not validation: + if len(kwargs) == 1: + sql_string = """SELECT * FROM {table_name} WHERE {field} = '{read_value}';""" + sql_string = sql_string.format(table_name=self.table_name, field=list(kwargs.keys())[0], read_value=list(kwargs.values())[0]) + elif len(kwargs) > 1: + inintial_string = """SELECT * FROM {table_name} WHERE """.format(table_name=self.table_name) + field_string = " AND ".join(["{field} = '{read_value}'".format(field=f[0],read_value=f[1]) for f in kwargs.items() ]) + ";" + sql_string = inintial_string + field_string + else: + print('[-] Provide At least One Argument') + return + + cur = self.db_conn.cursor() + cur.execute(sql_string) + self.db_conn.commit() + + #FETCHING DATA + result = cur.fetchall() + return self._parse_result(result) + else: + print('\n[-] READ DATA ERROR ...') + print('[-] {} IS NOT VALID FIELD NAME'.format(', '.join(validation))) + print('[-] CHOICES ARE {}'.format((', ').join(self.valid_fields))) + + def read_all(self): + #PART1 : CREATING THE SQL STRING + sql_string = """SELECT * FROM {table_name};""" + sql_string = sql_string.format(table_name=self.table_name) + + #PART2 : EXECUTING THAT CREARED STRING + cur = self.db_conn.cursor() + cur.execute(sql_string) + self.db_conn.commit() + + #FETCHING DATA + result = cur.fetchall() + return self._parse_result(result) + + def load(self): + self.connect_to_db() + if not self.table_exists(): + self.create_table() + self.load_table_info() + else: + self.load_table_info() + + +if __name__ == '__main__': + db_file_name = 'download_queue.db' + db_table = 'DownloadQueue' + db_fields = [ + ('id','integer','primary key','autoincrement'), + ('url','text'), + ('date','date'), + ('status','text'), + + ] + + db_obj = CustomSqliteAction(database_name=db_file_name, database_table=db_table, database_fields=db_fields) + + #will create .db file if not exists + #will create table if not exists + db_obj.load() + + #let's Store Some Data + #function > store_data() + #you can also use python datetime object as a date field + db_obj.store_data(url='https://m.youtube.com/video_id',date='2022-10-01') + + + #let's Update Some Data + #function > update_data() + db_obj.update_data(search_tuple=('id','1'), url='https://google.com/video_id') + + + #let's Read Some data + #function > read_data() , read_all() + + #read_data() + #-> will read based on single condition or multiple condition and returns python list contaning all the data + print('Single Argument Search ...') + data = db_obj.read_data(url='https://m.youtube.com/video_id') + print(data) + + print('Multiple Argument Search ...') + multi_con_data = db_obj.read_data(url='https://m.youtube.com/video_id',status='done') + print(multi_con_data) + + print('Reading All Data ...') + all_data = db_obj.read_all() + print(all_data) + + #let's delete Some Data + #function > delete_data() + delete_id = 1 + db_obj.delete_data(id=delete_id) + db_obj.delete_data(url='https://m.youtube.com/video_id') + db_obj.delete_data(url='https://m.youtube.com/video_id',status='done') diff --git a/Database-As-Storage/README.md b/Database-As-Storage/README.md new file mode 100644 index 0000000..b6fe02d --- /dev/null +++ b/Database-As-Storage/README.md @@ -0,0 +1,96 @@ +# Database-As-Storage + +### What is it ? + +-> Use MySQLite Database as Storage for any kind of python project and standalone scripts, without using any SQL Syntax and Programming, + +-> This Script provides CRUD Functionality using MySQLite and Python. + +### How to Use ? + +-> it's OOP based python Script so you can just called it in your script and use it. + +```python +from Database-As-Storage import CustomSqliteAction + +db_file_name = 'download_queue.db' +db_table = 'DownloadQueue' +db_fields = [ + ('id','integer','primary key','autoincrement'), + ('url','text'), + ('date','date'), + ('status','text'), + + ] + + db_obj = CustomSqliteAction(database_name=db_file_name, database_table=db_table, database_fields=db_fields) +``` + +-> this will create a file name `download_queue.db` if not exists + +-> will create table name `DownloadQueue` if not exists. + +-> will create that table will fields provides by `db_fields`. + +-> each field tuple ( field_name, other arguments .... ) , you can add other arguments which is allowed by SQL. + + + +## CURD FUNCTIONALITY + +### Creating Data / Storing Data into Database Table + +```python +db_obj.store_data(url='https://m.youtube.com/video_id',date='2022-10-01') +``` + +-> **FUNCTION** `store_data` + +-> provide arguments based on your table fields. + +-> this example code will store a database entry of value id = 1 (cause it's auto-incrementing ), url + +-> status will be Null in database cause you haven't provided it. + +### Updating Data Into Database Table + +```python +db_obj.update_data(search_tuple=('id','1'), url='https://google.com/video_id') +``` + +-> **FUNCTION** `update_data` + +-> will take one required argument to search for particular entry in database, + +-> and the value that you want to update. + +-> this example code will change the url of id 1. + +### Reading Data From Database Table + +```python +data = db_obj.read_data(url='https://m.youtube.com/video_id') +multi_con_data = db_obj.read_data(url='https://m.youtube.com/video_id',status='done') +all_data = db_obj.read_all() +``` + +-> **FUNCTION** `read_data` and `read_all` + +-> you can search and get data based on multiple or single arguments, + +-> also you can get the whole table also. + +-> will return list of python dictionary ( each row as dict object ) + +### Deleting Data From Database Table + +```python +delete_id = 1 +db_obj.delete_data(id=delete_id) +db_obj.delete_data(url='https://m.youtube.com/video_id') +db_obj.delete_data(url='https://m.youtube.com/video_id',status='done') +``` + +-> **FUNCTION** `delete_data` + +-> you can delete data based on multiple or single arguments. \ No newline at end of file diff --git a/Database-As-Storage/requirements.txt b/Database-As-Storage/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index 3d92761..1f1b93c 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ So far, the following projects have been integrated to this repo: | [CSV to Excel](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/CSV-to-Excel)|[xemeds](https://github.com/xemeds) | |[Current City Weather](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Current_City_Weather) |[Jesse Bridge](https://github.com/jessebridge) | |[Directory organizer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Directory-organizer) | [Athul P](https://github.com/athulpn) | +|[Database-As-Storage](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Database-As-Storage) | [Bhargav Kuvadiya](https://github.com/techdobz) | |[DOH DIG](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/DOH-Dig/) | [Ryan](https://github.com/awsumco) | |[English Theasaurus](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/English_Theasaurus/) | [Ansh Dhingra](https://github.com/anshdhinhgra47) | |[Elasticsearch snapshot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/elastic-snapshot) | [Joe Ryan](https://github.com/joeryan) | @@ -78,7 +79,7 @@ So far, the following projects have been integrated to this repo: |[Youtube video downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Youtube_Video_Downloader)|[Christopher He](https://github.com/hecris)| |[Zabbix API](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zabbix_api)|[msg4sunny](https://github.com/msg4sunny)| |[Zip password cracker](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zip_password_cracker)|[umar abdullahi](https://github.com/umarbrowser)| -|[RSA Algorithm](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/RSA_Algorithm)|[Chinmay Rane](https://github.com/Chinmayrane16) +|[RSA Algorithm](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/RSA_Algorithm)|[Chinmay Rane](https://github.com/Chinmayrane16)| |[CLI Calculator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/cli_calculator)|[Willian GL](https://github.com/williangl) | |[Find PhoneNumber in String](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Find-PhoneNumber-in-String)|[Austin Zuniga](https://github.com/AustinZuniga)| |[IMDB TV Series Info Extractor](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/imdb_episode_ratings)|[Yash Raj Sarrof](https://github.com/yashYRS) | @@ -164,21 +165,21 @@ So far, the following projects have been integrated to this repo: |[Random_Email_Generator](Random_Email_Generator)|[Shubham Garg](https://github.com/shub-garg)| |[WiFi Password Viewer](Wifi-Password)|[Sagar Patel](https://github.com/sagar627)| |[Tambola_Ticket_Generator](Tambola_Ticket_Generator)|[Amandeep_Singh](https://github.com/Synster)| -| [Py_Cleaner](Py_Cleaner) | [Abhishek Dobliyal](https://github.com/Abhishek-Dobliyal) +| [Py_Cleaner](Py_Cleaner) | [Abhishek Dobliyal](https://github.com/Abhishek-Dobliyal)| |[Send messages to sqs in parallel](send_sqs_messages_in_parallel)|[Jinam Shah](https://github.com/jinamshah)| |[Codeforces Checker](codeforcesChecker)|[Jinesh Parakh](https://github.com/jineshparakh)| -|[Github repo creator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Git_repo_creator)|[Harish Tiwari ](https://github.com/optimist2309) -|[Remove-Duplicate-Files](Remove-Duplicate-Files)|[Aayushi Varma](https://github.com/aayuv17) -|[PDF2text](PDF2text)|[QuangPH](https://github.com/quangph-1686a) -|[Image Watermarker (batch)](imageWatermarker)|[Remco Halman](https://github.com/remcohalman) +|[Github repo creator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Git_repo_creator)|[Harish Tiwari ](https://github.com/optimist2309)| +|[Remove-Duplicate-Files](Remove-Duplicate-Files)|[Aayushi Varma](https://github.com/aayuv17)| +|[PDF2text](PDF2text)|[QuangPH](https://github.com/quangph-1686a)| +|[Image Watermarker (batch)](imageWatermarker)|[Remco Halman](https://github.com/remcohalman)| |[Folder Manager](Folder_Manager)|[Harsh Raj](https://github.com/DeadProgrammer0)| -|[IMDBQuerier](IMDBQuerier)|[Burak Bekci](https://github.com/Bekci) -|[URL shortener](url_shortener)|[Sam Ebison](https://github.com/ebsa491) -|[2048](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/2048)|[Krunal](https://github.com/gitkp11) +|[IMDBQuerier](IMDBQuerier)|[Burak Bekci](https://github.com/Bekci)| +|[URL shortener](url_shortener)|[Sam Ebison](https://github.com/ebsa491)| +|[2048](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/2048)|[Krunal](https://github.com/gitkp11)| |[Spotify Downloader](spotify_downloader)|[Sagar Patel](https://github.com/sagar627)| -|[Download Page as PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Download-page-as-pdf)|[Jeremias Gomes](https://github.com/j3r3mias) -|[JSON file to YAML convertor](https://github.com/saksham117/Awesome-Python-Scripts/tree/master/json-to-yaml)|[Saksham Basandrai](https://github.com/saksham117) -|[Independent RSA Communication Algorithm](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/RSA_Communication)|[Miguel Santos](https://github.com/wi6n3l) +|[Download Page as PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Download-page-as-pdf)|[Jeremias Gomes](https://github.com/j3r3mias)| +|[JSON file to YAML convertor](https://github.com/saksham117/Awesome-Python-Scripts/tree/master/json-to-yaml)|[Saksham Basandrai](https://github.com/saksham117)| +|[Independent RSA Communication Algorithm](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/RSA_Communication)|[Miguel Santos](https://github.com/wi6n3l)| |[GithubBot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/GithubBot)|[Abhilasha](https://github.com/Abhilasha06)| |[Translate CLI](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/TranslateCLI)|[Rodrigo Oliveira](https://github.com/rodrigocam)| |[Rock-Paper-Scissor Game](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Rock-Paper-Scissor)|[Punit Sakre](https://github.com/punitsakre23)|