Package: pysql-manager
PysqlManagerA python package to manage sqlGETTING STARTED !Creating a PySql object is the first step. All function are defined in PySql Class (Which is base class for PysqlManager Module)For creating PySql object we need a meta_class / reference class (meta_class is nothing but a class structure for SQL table)User(id varchar(25), name varchar(20), Age INT)For above table , the reference class will befrompysql_manager.typesimportColumn,IntegerType,StringTypeclassUser:id=Column(col_type=IntegerType())name=Column(col_type=StringType(25))age=Column(col_type=IntegerType())__table__="User"# Now we can use this meta_class to create actual PySql objcetfrompysql_managerimportPySqlusers=PySql("localhost","root","passowrd","DB",User)users.fetch_all.show()#sample method for fetching and showing all the data from table UserFETCH ALL DATA FROM SQL TABLEfrompysql_manager.typesimportColumn,IntegerType,StringTypeclassUser:id=Column(col_type=IntegerType())name=Column(col_type=StringType(25))age=Column(col_type=IntegerType())__table__="User"frompysql_managerimportPySqlusers=PySql("localhost","root","passowrd","DB",User)users.fetch_all# Return PySqlConnectionfetch_all method will return a PySqlCollection object , which contain rich functionalities..show() - To show data in table formusers.fetch_all.show()# Return None.first() - Return first rowA single Row is nothing but an object of base class. For above example , each row will be an object of class User
means, we can accessrow.column(In this case row.age, row.id, row.name etc)users.fetch_all.first()# Return single meta_class object.last() - To get last rowusers.fetch_all.last()# Return single meta_class object.is_empty() - To get last rowusers.fetch_all.is_empty()# Return Boolean.count() - To get total count of rowsusers.fetch_all.count()# Return Integer.to_df() - Create pandas DataFrameColumn name defined in meta_class will be taken for Pandas DataFrame creationusers.fetch_all.to_df()# Return pandas DataFrame.to_list_dict() - Creates List of python dictionariesList of python dictionaries. Where each dictionary will be a SQL recordusers.fetch_all.to_list_dict()# Return List[dict].save_as_csv() - To save PySqlCollection object as CSV file.users.fetch_all.save_as_csv("path",delimiter="|")# Return None.select() - To select specific columns from PySqlCollectionusers.fetch_all.select(["age","id"])# Return PySqlCollectionSince this is also returning a PySqlCollection, this can be again chained with all above methods.Egusers.fetch_all.select(["age","id"]).count()users.fetch_all.select(["age","id"]).first()users.fetch_all.select(["age","id"]).last()users.fetch_all.select(["age","id"]).show()FILTER DATA FROM SQLFor filtering data from SQL using PySql-Manager just use the inbuilt filter() methodusers.filter("age > 10")# Return PySqlFilterObjfilter is a special method which will return a PySqlFilterObj which can be then used to fetch filtered data
(which will return same PySqlCollection when using fetch_all() - fetch_all will return all data from SQL, but filter().fetch_filtered will return filtered data)
or can be used to update, or delete filtered data.fetch_filtered - To get PySqlCollection of filtered SQL datausers.filter("age > 10").fetch_filtered# Return PySqlCollection.update() - To update filtered datausers.filter("age > 10").update(nam="newName",age="12")# Return None.delete() - To delete filtered datausers.filter("age > 10").delete()# Return NoneINSERT DATA TO SQL TABLEInsert is done using .insert() method, The data should be List of python dictionaries.frompysql_manager.typesimportColumn,IntegerType,StringTypeclassUser:id=Column(col_type=IntegerType())name=Column(col_type=StringType(25))age=Column(col_type=IntegerType())__table__="User"frompysql_managerimportPySqlusers=PySql("localhost","root","passowrd","DB",User)sql_data=[{"id":1,"name":"user1","age":22},{"id":2,"name":"user2","age":12}]users.insert(sql_data)# Return PySql selfIf there is duplicate entry for primary key (In this caseidcolumn, it will raisePRIMARY KEY ERROR). To avoid this and update on duplicate key you can useupdate_on_duplicateargument and pass list columns you need to update when there is a duplicate entry.frompysql_manager.typesimportColumn,IntegerType,StringTypeclassUser:id=Column(col_type=IntegerType())name=Column(col_type=StringType(25))age=Column(col_type=IntegerType())__table__="User"frompysql_managerimportPySqlusers=PySql("localhost","root","passowrd","DB",User)sql_data=[{"id":1,"name":"user1","age":22},{"id":2,"name":"user2","age":12}]users.insert(sql_data,update_on_duplicate=["age"])# Return PySql self