Skip to content

Commit dcece5c

Browse files
committed
Added Database-As-Storage & Updated Main README.md
1 parent 4f56c75 commit dcece5c

File tree

4 files changed

+321
-12
lines changed

4 files changed

+321
-12
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
from dataclasses import field, fields
2+
import sqlite3
3+
from unittest import result
4+
5+
class CustomSqliteAction():
6+
def __init__(self,database_name, database_table, database_fields):
7+
self.database = database_name
8+
self.table_name = database_table
9+
self.fields = database_fields
10+
11+
self.db_conn = None
12+
self.valid_fields = None
13+
14+
def connect_to_db(self):
15+
print('[+] Connecting to {}'.format(self.database))
16+
db_conn = sqlite3.connect(self.database)
17+
self.db_conn = db_conn
18+
19+
def load_table_info(self):
20+
self.valid_fields = [f_name[0] for f_name in self.fields]
21+
22+
def table_validation(self,inserted_fields):
23+
return list(set(inserted_fields).difference(set(self.valid_fields)))
24+
25+
def _parse_result(self,db_data):
26+
query_set = []
27+
for data in db_data:
28+
data_dict = {k: v for k, v in zip(self.valid_fields, data)}
29+
query_set.append(data_dict)
30+
return query_set
31+
32+
def create_table(self):
33+
sql_string = """CREATE TABLE IF NOT EXISTS {table_name} {field_string};"""
34+
field_string = "("+", ".join([" ".join(fi for fi in f) for f in self.fields])+")"
35+
sql_string = sql_string.format(table_name=self.table_name,field_string=field_string)
36+
37+
print("[+] Creating Table {} .....\n".format(self.table_name))
38+
cur = self.db_conn.cursor()
39+
cur.execute(sql_string)
40+
41+
def table_exists(self):
42+
sql_string = """SELECT * FROM {}""".format(self.table_name)
43+
try:
44+
cur = self.db_conn.cursor()
45+
cur.execute(sql_string)
46+
print('[+] Connecting To Table {}\n'.format(self.table_name))
47+
return True
48+
except sqlite3.OperationalError:
49+
print('[-] TABLE NAME {} DOES NOT EXISTS'.format(self.table_name))
50+
return False
51+
52+
def store_data(self,**kwargs):
53+
validation = self.table_validation(kwargs.keys())
54+
if not validation:
55+
sql_string = """INSERT INTO {table_name} {field_string} VALUES {value_string};"""
56+
field_string = "("+", ".join([f for f in kwargs.keys()])+")"
57+
value_string = "("+ ", ".join([f"'{v}'" for v in kwargs.values()]) +")"
58+
59+
sql_string = sql_string.format(table_name=self.table_name,field_string=field_string,value_string=value_string)
60+
cur = self.db_conn.cursor()
61+
try:
62+
cur.execute(sql_string)
63+
except sqlite3.OperationalError:
64+
print('[-] Database Syntax Error probabily because of \' in data ')
65+
self.db_conn.commit()
66+
else:
67+
print('\n[-] STORE DATA ERROR ...')
68+
print('[-] {} IS NOT VALID FIELD NAME'.format(', '.join(validation)))
69+
print('[-] CHOICES ARE {}'.format((', ').join(self.valid_fields)))
70+
71+
def delete_data(self,**kwargs):
72+
validation = self.table_validation(kwargs.keys())
73+
if not validation:
74+
if len(kwargs) == 1:
75+
sql_string = """DELETE FROM {table_name} WHERE {field} = '{field_id}';"""
76+
sql_string = sql_string.format(table_name=self.table_name,field=list(kwargs.keys())[0],field_id=list(kwargs.values())[0])
77+
elif len(kwargs) > 1:
78+
inintial_string = """DELETE FROM {table_name} WHERE """.format(table_name=self.table_name)
79+
field_string = " AND ".join(["{field} = '{field_value}'".format(field=f[0],field_value=f[1]) for f in kwargs.items() ]) + ";"
80+
sql_string = inintial_string + field_string
81+
else:
82+
print('[-] At least Provide 1 Argument')
83+
return
84+
85+
cur = self.db_conn.cursor()
86+
cur.execute(sql_string)
87+
self.db_conn.commit()
88+
print("[+] Delete Data Successfully")
89+
90+
else:
91+
print('\n[-] DELETE DATA ERROR ...')
92+
print('[-] {} IS NOT VALID FIELD NAME'.format(', '.join(validation)))
93+
print('[-] CHOICES ARE {}'.format((', ').join(self.valid_fields)))
94+
95+
def update_data(self,search_tuple, **kwargs):
96+
validation = self.table_validation(kwargs.keys())
97+
if not validation:
98+
if len(kwargs) == 1:
99+
sql_string = """UPDATE {table_name} SET {field} = '{update_value}' WHERE {p_field} = {field_id};"""
100+
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])
101+
else:
102+
print('[-] Only One Upadte Argument Allowed')
103+
return
104+
cur = self.db_conn.cursor()
105+
cur.execute(sql_string)
106+
self.db_conn.commit()
107+
print("[+] Update Data Successfully")
108+
else:
109+
print('\n[-] DELETE DATA ERROR ...')
110+
print('[-] {} IS NOT VALID FIELD NAME'.format(', '.join(validation)))
111+
print('[-] CHOICES ARE {}'.format((', ').join(self.valid_fields)))
112+
113+
def read_data(self,**kwargs):
114+
validation = self.table_validation(kwargs.keys())
115+
if not validation:
116+
if len(kwargs) == 1:
117+
sql_string = """SELECT * FROM {table_name} WHERE {field} = '{read_value}';"""
118+
sql_string = sql_string.format(table_name=self.table_name, field=list(kwargs.keys())[0], read_value=list(kwargs.values())[0])
119+
elif len(kwargs) > 1:
120+
inintial_string = """SELECT * FROM {table_name} WHERE """.format(table_name=self.table_name)
121+
field_string = " AND ".join(["{field} = '{read_value}'".format(field=f[0],read_value=f[1]) for f in kwargs.items() ]) + ";"
122+
sql_string = inintial_string + field_string
123+
else:
124+
print('[-] Provide At least One Argument')
125+
return
126+
127+
cur = self.db_conn.cursor()
128+
cur.execute(sql_string)
129+
self.db_conn.commit()
130+
131+
#FETCHING DATA
132+
result = cur.fetchall()
133+
return self._parse_result(result)
134+
else:
135+
print('\n[-] READ DATA ERROR ...')
136+
print('[-] {} IS NOT VALID FIELD NAME'.format(', '.join(validation)))
137+
print('[-] CHOICES ARE {}'.format((', ').join(self.valid_fields)))
138+
139+
def read_all(self):
140+
#PART1 : CREATING THE SQL STRING
141+
sql_string = """SELECT * FROM {table_name};"""
142+
sql_string = sql_string.format(table_name=self.table_name)
143+
144+
#PART2 : EXECUTING THAT CREARED STRING
145+
cur = self.db_conn.cursor()
146+
cur.execute(sql_string)
147+
self.db_conn.commit()
148+
149+
#FETCHING DATA
150+
result = cur.fetchall()
151+
return self._parse_result(result)
152+
153+
def load(self):
154+
self.connect_to_db()
155+
if not self.table_exists():
156+
self.create_table()
157+
self.load_table_info()
158+
else:
159+
self.load_table_info()
160+
161+
162+
if __name__ == '__main__':
163+
db_file_name = 'download_queue.db'
164+
db_table = 'DownloadQueue'
165+
db_fields = [
166+
('id','integer','primary key','autoincrement'),
167+
('url','text'),
168+
('date','date'),
169+
('status','text'),
170+
171+
]
172+
173+
db_obj = CustomSqliteAction(database_name=db_file_name, database_table=db_table, database_fields=db_fields)
174+
175+
#will create .db file if not exists
176+
#will create table if not exists
177+
db_obj.load()
178+
179+
#let's Store Some Data
180+
#function > store_data()
181+
#you can also use python datetime object as a date field
182+
db_obj.store_data(url='https://m.youtube.com/video_id',date='2022-10-01')
183+
184+
185+
#let's Update Some Data
186+
#function > update_data()
187+
db_obj.update_data(search_tuple=('id','1'), url='https://google.com/video_id')
188+
189+
190+
#let's Read Some data
191+
#function > read_data() , read_all()
192+
193+
#read_data()
194+
#-> will read based on single condition or multiple condition and returns python list contaning all the data
195+
print('Single Argument Search ...')
196+
data = db_obj.read_data(url='https://m.youtube.com/video_id')
197+
print(data)
198+
199+
print('Multiple Argument Search ...')
200+
multi_con_data = db_obj.read_data(url='https://m.youtube.com/video_id',status='done')
201+
print(multi_con_data)
202+
203+
print('Reading All Data ...')
204+
all_data = db_obj.read_all()
205+
print(all_data)
206+
207+
#let's delete Some Data
208+
#function > delete_data()
209+
delete_id = 1
210+
db_obj.delete_data(id=delete_id)
211+
db_obj.delete_data(url='https://m.youtube.com/video_id')
212+
db_obj.delete_data(url='https://m.youtube.com/video_id',status='done')

Database-As-Storage/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Database-As-Storage
2+
3+
### What is it ?
4+
5+
-> Use MySQLite Database as Storage for any kind of python project and standalone scripts, without using any SQL Syntax and Programming,
6+
7+
-> This Script provides CRUD Functionality using MySQLite and Python.
8+
9+
### How to Use ?
10+
11+
-> it's OOP based python Script so you can just called it in your script and use it.
12+
13+
```python
14+
from Database-As-Storage import CustomSqliteAction
15+
16+
db_file_name = 'download_queue.db'
17+
db_table = 'DownloadQueue'
18+
db_fields = [
19+
('id','integer','primary key','autoincrement'),
20+
('url','text'),
21+
('date','date'),
22+
('status','text'),
23+
24+
]
25+
26+
db_obj = CustomSqliteAction(database_name=db_file_name, database_table=db_table, database_fields=db_fields)
27+
```
28+
29+
-> this will create a file name `download_queue.db` if not exists
30+
31+
-> will create table name `DownloadQueue` if not exists.
32+
33+
-> will create that table will fields provides by `db_fields`.
34+
35+
-> each field tuple ( field_name, other arguments .... ) , you can add other arguments which is allowed by SQL.
36+
37+
38+
39+
## CURD FUNCTIONALITY
40+
41+
### Creating Data / Storing Data into Database Table
42+
43+
```python
44+
db_obj.store_data(url='https://m.youtube.com/video_id',date='2022-10-01')
45+
```
46+
47+
-> **FUNCTION** `store_data`
48+
49+
-> provide arguments based on your table fields.
50+
51+
-> this example code will store a database entry of value id = 1 (cause it's auto-incrementing ), url
52+
53+
-> status will be Null in database cause you haven't provided it.
54+
55+
### Updating Data Into Database Table
56+
57+
```python
58+
db_obj.update_data(search_tuple=('id','1'), url='https://google.com/video_id')
59+
```
60+
61+
-> **FUNCTION** `update_data`
62+
63+
-> will take one required argument to search for particular entry in database,
64+
65+
-> and the value that you want to update.
66+
67+
-> this example code will change the url of id 1.
68+
69+
### Reading Data From Database Table
70+
71+
```python
72+
data = db_obj.read_data(url='https://m.youtube.com/video_id')
73+
multi_con_data = db_obj.read_data(url='https://m.youtube.com/video_id',status='done')
74+
all_data = db_obj.read_all()
75+
```
76+
77+
-> **FUNCTION** `read_data` and `read_all`
78+
79+
-> you can search and get data based on multiple or single arguments,
80+
81+
-> also you can get the whole table also.
82+
83+
-> will return list of python dictionary ( each row as dict object )
84+
85+
### Deleting Data From Database Table
86+
87+
```python
88+
delete_id = 1
89+
db_obj.delete_data(id=delete_id)
90+
db_obj.delete_data(url='https://m.youtube.com/video_id')
91+
db_obj.delete_data(url='https://m.youtube.com/video_id',status='done')
92+
```
93+
94+
-> **FUNCTION** `delete_data`
95+
96+
-> you can delete data based on multiple or single arguments.

Database-As-Storage/requirements.txt

Whitespace-only changes.

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ So far, the following projects have been integrated to this repo:
3232
| [CSV to Excel](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/CSV-to-Excel)|[xemeds](https://github.com/xemeds) |
3333
|[Current City Weather](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Current_City_Weather) |[Jesse Bridge](https://github.com/jessebridge) |
3434
|[Directory organizer](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Directory-organizer) | [Athul P](https://github.com/athulpn) |
35+
|[Database-As-Storage](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Database-As-Storage) | [Bhargav Kuvadiya](https://github.com/techdobz) |
3536
|[DOH DIG](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/DOH-Dig/) | [Ryan](https://github.com/awsumco) |
3637
|[English Theasaurus](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/English_Theasaurus/) | [Ansh Dhingra](https://github.com/anshdhinhgra47) |
3738
|[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:
7879
|[Youtube video downloader](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Youtube_Video_Downloader)|[Christopher He](https://github.com/hecris)|
7980
|[Zabbix API](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zabbix_api)|[msg4sunny](https://github.com/msg4sunny)|
8081
|[Zip password cracker](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/zip_password_cracker)|[umar abdullahi](https://github.com/umarbrowser)|
81-
|[RSA Algorithm](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/RSA_Algorithm)|[Chinmay Rane](https://github.com/Chinmayrane16)
82+
|[RSA Algorithm](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/RSA_Algorithm)|[Chinmay Rane](https://github.com/Chinmayrane16)|
8283
|[CLI Calculator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/cli_calculator)|[Willian GL](https://github.com/williangl) |
8384
|[Find PhoneNumber in String](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Find-PhoneNumber-in-String)|[Austin Zuniga](https://github.com/AustinZuniga)|
8485
|[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:
164165
|[Random_Email_Generator](Random_Email_Generator)|[Shubham Garg](https://github.com/shub-garg)|
165166
|[WiFi Password Viewer](Wifi-Password)|[Sagar Patel](https://github.com/sagar627)|
166167
|[Tambola_Ticket_Generator](Tambola_Ticket_Generator)|[Amandeep_Singh](https://github.com/Synster)|
167-
| [Py_Cleaner](Py_Cleaner) | [Abhishek Dobliyal](https://github.com/Abhishek-Dobliyal)
168+
| [Py_Cleaner](Py_Cleaner) | [Abhishek Dobliyal](https://github.com/Abhishek-Dobliyal)|
168169
|[Send messages to sqs in parallel](send_sqs_messages_in_parallel)|[Jinam Shah](https://github.com/jinamshah)|
169170
|[Codeforces Checker](codeforcesChecker)|[Jinesh Parakh](https://github.com/jineshparakh)|
170-
|[Github repo creator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Git_repo_creator)|[Harish Tiwari ](https://github.com/optimist2309)
171-
|[Remove-Duplicate-Files](Remove-Duplicate-Files)|[Aayushi Varma](https://github.com/aayuv17)
172-
|[PDF2text](PDF2text)|[QuangPH](https://github.com/quangph-1686a)
173-
|[Image Watermarker (batch)](imageWatermarker)|[Remco Halman](https://github.com/remcohalman)
171+
|[Github repo creator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Git_repo_creator)|[Harish Tiwari ](https://github.com/optimist2309)|
172+
|[Remove-Duplicate-Files](Remove-Duplicate-Files)|[Aayushi Varma](https://github.com/aayuv17)|
173+
|[PDF2text](PDF2text)|[QuangPH](https://github.com/quangph-1686a)|
174+
|[Image Watermarker (batch)](imageWatermarker)|[Remco Halman](https://github.com/remcohalman)|
174175
|[Folder Manager](Folder_Manager)|[Harsh Raj](https://github.com/DeadProgrammer0)|
175-
|[IMDBQuerier](IMDBQuerier)|[Burak Bekci](https://github.com/Bekci)
176-
|[URL shortener](url_shortener)|[Sam Ebison](https://github.com/ebsa491)
177-
|[2048](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/2048)|[Krunal](https://github.com/gitkp11)
176+
|[IMDBQuerier](IMDBQuerier)|[Burak Bekci](https://github.com/Bekci)|
177+
|[URL shortener](url_shortener)|[Sam Ebison](https://github.com/ebsa491)|
178+
|[2048](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/2048)|[Krunal](https://github.com/gitkp11)|
178179
|[Spotify Downloader](spotify_downloader)|[Sagar Patel](https://github.com/sagar627)|
179-
|[Download Page as PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Download-page-as-pdf)|[Jeremias Gomes](https://github.com/j3r3mias)
180-
|[JSON file to YAML convertor](https://github.com/saksham117/Awesome-Python-Scripts/tree/master/json-to-yaml)|[Saksham Basandrai](https://github.com/saksham117)
181-
|[Independent RSA Communication Algorithm](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/RSA_Communication)|[Miguel Santos](https://github.com/wi6n3l)
180+
|[Download Page as PDF](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Download-page-as-pdf)|[Jeremias Gomes](https://github.com/j3r3mias)|
181+
|[JSON file to YAML convertor](https://github.com/saksham117/Awesome-Python-Scripts/tree/master/json-to-yaml)|[Saksham Basandrai](https://github.com/saksham117)|
182+
|[Independent RSA Communication Algorithm](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/RSA_Communication)|[Miguel Santos](https://github.com/wi6n3l)|
182183
|[GithubBot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/GithubBot)|[Abhilasha](https://github.com/Abhilasha06)|
183184
|[Translate CLI](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/TranslateCLI)|[Rodrigo Oliveira](https://github.com/rodrigocam)|
184185
|[Rock-Paper-Scissor Game](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Rock-Paper-Scissor)|[Punit Sakre](https://github.com/punitsakre23)|

0 commit comments

Comments
 (0)