├── .gitignore ├── README.md ├── Screenshots ├── ss1.jpeg ├── ss2.jpeg ├── ss3.jpeg └── ss4.jpeg ├── pypackage ├── FrontServer │ ├── __init__.py │ ├── server.py │ └── templates │ │ ├── history.html │ │ ├── index.html │ │ ├── newconnection.html │ │ └── try_query.html ├── __init__.py └── s2j.py ├── server ├── cruds │ ├── __init__.py │ └── query.py └── main.py └── test.py /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | venv/*.* 3 | venv/* 4 | .vscode 5 | *.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

SQLtoJSON

2 |
3 |
4 |     5 | 6 |
7 |
8 |

Meet this amazing middleware for your MySQL operations.

9 | 10 | Just a Single line to connect and single line to execute, and boom! You have your response in JSON.
11 | Say Good Bye to Cursors, Connections, MySQL Drivers. This package has everything covered. And that is not the best part.
12 | 13 | Best part is you get an easy to use web interface where you can see your "in-code" query history and try new queries. 14 | Wanna change DB connection? Again, this package has got you covered. 15 | 16 |

Usage

17 | 18 | ```Python 19 | from pypackage import s2j 20 | 21 | # This line connnects you to your database. All exceptions self handled. ;) 22 | db = s2j.s2j("localhost", "root", "password", "database") 23 | 24 | # Now one liners for your queries!!!!!! 25 | response = db.execQuery("SELECT * FROM users;") 26 | 27 | # You get an array in data field containing data as indivisual dictionary objects, key being column name. 28 | print(response['data']) 29 | 30 | # Use this method to fireup Web Interface. And the server will be initiated on PORT 4001 31 | s2j.start_lookup_server(db) 32 | ``` 33 | 34 |

Screenshots of Web Interfaces

35 |
36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Screenshots/ss1.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amandesai01/SQLtoJSON/b11f275c788b4f4c71d187fb35539f348080a37b/Screenshots/ss1.jpeg -------------------------------------------------------------------------------- /Screenshots/ss2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amandesai01/SQLtoJSON/b11f275c788b4f4c71d187fb35539f348080a37b/Screenshots/ss2.jpeg -------------------------------------------------------------------------------- /Screenshots/ss3.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amandesai01/SQLtoJSON/b11f275c788b4f4c71d187fb35539f348080a37b/Screenshots/ss3.jpeg -------------------------------------------------------------------------------- /Screenshots/ss4.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amandesai01/SQLtoJSON/b11f275c788b4f4c71d187fb35539f348080a37b/Screenshots/ss4.jpeg -------------------------------------------------------------------------------- /pypackage/FrontServer/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amandesai01/SQLtoJSON/b11f275c788b4f4c71d187fb35539f348080a37b/pypackage/FrontServer/__init__.py -------------------------------------------------------------------------------- /pypackage/FrontServer/server.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, redirect, url_for 2 | import pickle 3 | from flask_wtf import FlaskForm 4 | from wtforms import StringField, SubmitField, PasswordField 5 | 6 | app = Flask(__name__) 7 | app.secret_key = "KKKKEEEEYYY" 8 | # s2jObj = None 9 | class queryForm(FlaskForm): 10 | Query = StringField() 11 | Submit = SubmitField('Run') 12 | 13 | class newConnForm(FlaskForm): 14 | host = StringField() 15 | username = StringField() 16 | password = PasswordField() 17 | database = StringField() 18 | connect = SubmitField('Connect') 19 | 20 | @app.route('/new', methods = ['GET', 'POST']) 21 | def new(): 22 | form = newConnForm() 23 | if form.is_submitted(): 24 | # backupS2JInstance =pickle.dumps(s2jObj) 25 | s2jObj.updateHost(form.host.data) 26 | s2jObj.updateUsername(form.username.data) 27 | s2jObj.updatePassword(form.password.data) 28 | s2jObj.updateDatabase(form.database.data) 29 | s2jObj.resetHistory() 30 | if s2jObj.checkConnection(): 31 | return redirect(url_for('index')) 32 | else: 33 | #restoring old version 34 | # s2jObj = pickle.loads(backupS2JInstance) 35 | return render_template('newconnection.html', error=True, form=form) 36 | return render_template('newconnection.html', error=False, form=form) 37 | 38 | @app.route('/querytab', methods=["POST", "GET"]) 39 | def querytab(): 40 | form = queryForm() 41 | if form.is_submitted(): 42 | query = form.Query.data 43 | resp = s2jObj.execQuery(query) 44 | return render_template('try_query.html', form=form, isRes=True, res=resp) 45 | return render_template('try_query.html', form=form, isRes=False, res="") 46 | 47 | @app.route("/history") 48 | def history(): 49 | # print(s2jObj.get_history()) 50 | return render_template('history.html', history = s2jObj.get_history()) 51 | 52 | @app.route("/") 53 | def index(): 54 | # print(s2jObj.get_history()) 55 | return render_template('index.html') 56 | 57 | def start(pick): 58 | global s2jObj 59 | s2jObj = pickle.loads(pick) 60 | print(s2jObj) 61 | if s2jObj: 62 | app.run(port=4001) 63 | 64 | def update_pickle(pick): 65 | s2jObj = pickle.loads(pick) -------------------------------------------------------------------------------- /pypackage/FrontServer/templates/history.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | History 7 | 8 | 9 | 10 |
11 |


12 | < Back 13 |

14 |
15 |

History

16 |

17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | {% for data in history %} 26 | 27 | 34 | 35 | 36 | {% endfor %} 37 | 38 |
StatusQuery
28 | {% if data.success %} 29 | 30 | {% else %} 31 | 32 | {% endif %} 33 | {{ data.query }}
39 |
40 |
41 | 42 | -------------------------------------------------------------------------------- /pypackage/FrontServer/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Home 7 | 8 | 9 | 10 |
11 |






12 |
13 | Logout 14 |
15 |
16 |

SQLtoJSON

17 |

Welcome to SQLtoJSON. Click any of the option below.

18 | Try Queries

19 | View History 20 |
21 |
22 |
23 |
24 | 25 | -------------------------------------------------------------------------------- /pypackage/FrontServer/templates/newconnection.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Home 7 | 8 | 9 | 10 |
11 |






12 |
13 |
14 | {% if error %} 15 | 18 | {% endif %} 19 |
20 |

Create Connection

21 |
22 | {{ form.hidden_tag() }} 23 | {{ form.csrf_token }} 24 |
25 | 26 | 27 | 28 | 29 | {{ form.host(class="form-control form-control-lg", placeholder="Host") }} 30 | {{ form.username(class="form-control form-control-lg", placeholder="Username") }} 31 | {{ form.password(class="form-control form-control-lg", placeholder="Password") }} 32 | {{ form.database(class="form-control form-control-lg", placeholder="Database") }} 33 | 34 | {{ form.connect(class="btn btn-dark") }} 35 | 36 |
37 |
38 |
39 |
40 |
41 |
42 | 43 | -------------------------------------------------------------------------------- /pypackage/FrontServer/templates/try_query.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | History 7 | 8 | 9 | 10 |
11 |


12 | < Back 13 |

14 |
15 |

Try a Query

16 |

17 |
18 | {{ form.hidden_tag() }} 19 | {{ form.csrf_token }} 20 |
21 | 22 | 23 | {{ res }} 24 | 25 | 26 | {{ form.Query(class="form-control form-control-lg", placeholder="Query") }} 27 | 28 | {{ form.Submit(class="btn btn-dark") }} 29 | 30 |
31 |
32 |
33 |
34 | 35 | -------------------------------------------------------------------------------- /pypackage/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amandesai01/SQLtoJSON/b11f275c788b4f4c71d187fb35539f348080a37b/pypackage/__init__.py -------------------------------------------------------------------------------- /pypackage/s2j.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | import pickle 4 | from pypackage.FrontServer import server 5 | 6 | class AuthErrorException(Exception): 7 | """ Access Denied for given set of credentials """ 8 | pass 9 | 10 | class ServerError(Exception): 11 | """ Error while communication to db""" 12 | pass 13 | 14 | class s2j: 15 | def __init__(self, host, username, password, database): 16 | self.__username = username 17 | self.__password = password 18 | self.__host = host 19 | self.__database = database 20 | # self.__authenticate(host, username, password, database) 21 | self.__history = [] 22 | 23 | def updateUsername(self, username): 24 | self.__username = username 25 | 26 | def updatePassword(self, password): 27 | self.__password = password 28 | 29 | def updateHost(self, host): 30 | self.__host = host 31 | 32 | def updateDatabase(self, database): 33 | self.__database = database 34 | 35 | def resetHistory(self): 36 | self.__history = [] 37 | 38 | def execQuery(self, query): 39 | data = {'query' : query, "username" : self.__username, "password" : self.__password, "host": self.__host, "database" : self.__database} 40 | # headers = {'token' : self.__header} 41 | rawres = requests.post("http://127.0.0.1:4909/execQuery", json = data).json() 42 | history_element = {} 43 | history_element['query'] = query 44 | history_element['success'] = False 45 | history_element['response'] = None 46 | history_element['error'] = None 47 | if rawres.get('status') == "failure" or rawres.get('status') == "error": 48 | print(rawres.get("error_message")) 49 | history_element['error'] = rawres.get("error_message") 50 | self.__history.append(history_element) 51 | return None 52 | history_element['success'] = True 53 | history_element['response'] = rawres.get("data") 54 | self.__history.append(history_element) 55 | update_pickle(self) 56 | return rawres 57 | 58 | def checkConnection(self): 59 | data = {"username" : self.__username, "password" : self.__password, "host": self.__host, "database" : self.__database} 60 | urawres = requests.post("http://127.0.0.1:4909/check", json = data) 61 | rawres = {} 62 | try: 63 | rawres = urawres.json() 64 | except Exception as e: 65 | print(e) 66 | print(rawres) 67 | if rawres.get('status') and rawres.get('status') == "failure": 68 | return False 69 | else: 70 | return True 71 | 72 | def get_history(self): 73 | return self.__history 74 | 75 | def start_lookup_server(s2jInstance): 76 | pick = pickle.dumps(s2jInstance) 77 | server.start(pick) 78 | 79 | def update_pickle(s2jInstance): 80 | server.update_pickle(pickle.dumps(s2jInstance)) -------------------------------------------------------------------------------- /server/cruds/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amandesai01/SQLtoJSON/b11f275c788b4f4c71d187fb35539f348080a37b/server/cruds/__init__.py -------------------------------------------------------------------------------- /server/cruds/query.py: -------------------------------------------------------------------------------- 1 | import mysql.connector 2 | from mysql.connector import Error 3 | import json 4 | 5 | def exec_query(quer, database, host, user, password): 6 | maindata = {"data": [], "error": ""} 7 | connection = None 8 | try: 9 | connection = mysql.connector.connect(host=host,database=database,user=user,password=password, use_pure = False) 10 | 11 | if connection.is_connected(): 12 | cursor = connection.cursor() 13 | cursor.execute(quer) 14 | ques = quer.lower().split() 15 | if ques[0] in {'desc', 'show', 'select'}: 16 | #cursor.execute("DESC Laptop") 17 | row = cursor.fetchone() 18 | num_fields = len(cursor.description) 19 | field_names = [i[0] for i in cursor.description] 20 | # col_list=[] 21 | # print(field_names) 22 | val = [] 23 | while row is not None: 24 | da = {} 25 | 26 | for k in range(len(row)): 27 | da[field_names[k]] = str(row[k]) 28 | maindata['data'].append(da) 29 | row = cursor.fetchone() 30 | maindata['status'] = 'ok' 31 | # print(re) 32 | 33 | # except Error: 34 | # print("Error while connecting to mysql",Error) 35 | # maindata['result'] = 'failure' 36 | except Error as e: 37 | maindata['error'] = str(e) 38 | maindata['status'] = "error" 39 | print(e) 40 | 41 | finally: 42 | if connection: 43 | if connection.is_connected(): 44 | cursor.close() 45 | connection.close() 46 | return maindata 47 | 48 | def checkConnection(database, host, user, password): 49 | connection = None 50 | try: 51 | connection = mysql.connector.connect(host=host,database=database,user=user,password=password, use_pure = False) 52 | return True 53 | except: 54 | return False 55 | finally: 56 | if connection: 57 | if connection.is_connected(): 58 | connection.close() -------------------------------------------------------------------------------- /server/main.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, jsonify 2 | import json 3 | import requests 4 | from cruds.query import exec_query, checkConnection 5 | app = Flask(__name__) 6 | 7 | @app.route('/execQuery', methods = ["POST", "GET"]) 8 | def execQuery(): 9 | data = request.get_json() 10 | database = data.get('database') 11 | host = data.get('host') 12 | username = data.get('username') 13 | password = data.get('password') 14 | query = data.get('query') 15 | print(database, username, password, host) 16 | try: 17 | return jsonify(exec_query(query, database, host, username, password)) 18 | except Exception as e: 19 | return jsonify({ "status" : "failure", "error_message" : str(e) }) 20 | 21 | @app.route('/check', methods = ["POST", "GET"]) 22 | def check(): 23 | data = request.get_json() 24 | database = data.get('database') 25 | host = data.get('host') 26 | username = data.get('username') 27 | password = data.get('password') 28 | if checkConnection(database, host, username, password): 29 | return jsonify({"status" : "ok"}) 30 | return jsonify({"status" : "failure"}) 31 | 32 | if __name__ == "__main__": 33 | app.run(port = 4909, debug = True) -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | from pypackage import s2j 2 | 3 | db = s2j.s2j("localhost", "root", "root@1441", "proj") 4 | print(db.execQuery("SELECT * FROM users;")) 5 | print(db.execQuery("SELECT * FROM usersss;")) 6 | s2j.start_lookup_server(db) --------------------------------------------------------------------------------