Package: pySqlServer
PySQLPySQL is a Python library for interacting with SQL Server databases. It provides a simplified interface for common database operations.## InstallationpySQL requires the following packages:pandasnumpysqlalchemypyodbcpipinstallpandasnumpysqlalchemypyodbcUsageImport PySQL and Table_analyzer:frompySqlServerimportPySQL,Table_analyzerTable_analyzer usage:A helper class for analyzing Pandas DataFrames to determine optimal SQL datatypes.df=pd.read_csv('test.csv')TA=Table_analyzer()dtype_dict=TA.analyze(df,texts_buffer=0.2)for texts_buffer > 1 :  N in nvarchar(N) sets to len_max_length + texts_bufferfor 0 < texts_buffer < 1 :  N in nvarchar(N) sets to len_max_length + texts_buffer*len_max_length (extra percentage)PySQL usage:Create a connectionpysql=PySQL()pysql.create_connection(server='host_ip',database='mydb',username='myuser',password='mypassword',local_sql=False)your user must have 'db_datareader', 'db_datawriter', 'db_ddlAdmin' permissions to module works perfectly
set local_sql=True for windows auth in local SQL Serverat first use (for every tables) you must call create_dtypes()dtype_dict={'col1':sql.sqltypes.INTEGER,'col2':sql.sqltypes.NVARCHAR(100)}# it's suggested to use Table_analyzer to calculate optimal dtype_dict# dtype_dict = TA.analyze(df,texts_buffer=0.2)pysql.create_dtypes(dtype_dict=dtype_dict,table_name='Test_table',schema='Test_schema')at all next usages you must call load_dtypes and next you can use to_sql method to send datapysql.load_dtypes(table_name='Test_table',schema='Test_schema')# created beforepysql.to_sql(df,'Test_table',schema='Test_schema',if_exists='append',text_cutter=True,date_normalizer=True,method='multi',verbos=True)you can use primary_key='column_name' to set tables primary_keyin next usages it's not allowed to use this'text_cutter' trys to cut new text if those length was taller than column capacity'date_normalizer' trys to make date format colums suitable for sql servermethod='multi' for insert multi row in ine queryverbos=True --> show progress bar for your data transfer (default:False)and there is some read data methods in order to read data from your database (returns pandas dataframe)pysql.tables_list(schema=None)pysql.read_sql_table(table_name,schema=None)pysql.read_sql_query(query='SELECT * FROM TABLE_NAME')also you can update your table with a update_key and update_valuethis line should update age of someone in the table named Bob Lookie to 83 YO!
in other way you can set update_key and update_value with list of dicts and it update all queries in a row (len(update_key) should be equal to len(update_value))pysql.update_table(table_name='personal_data',schema='persons',update_key={'name':'Bob','last_name':'Looki'},update_value={'age':83})logger is a common method to use ( it logs datetime and process_id and actor_user in order to make your actions trackable )every to_sql calls can join with logs with 'process_id' column , so you can find who and when it's started to store data and how long takes it processpysql.logger('create_connection','success','connected')auto log system logs all your method calls  like bellow sample:auto_log sample:functionstatelogconnection_userprocess_iddatetimecreate_connectionsuccessconnectedpysql_user-12023-08-12 16:04:22.000read_sql_tablestartsuccesspysql_user02023-08-12 16:04:22.000read_sql_tableendsuccesspysql_user02023-08-12 16:04:23.000create_dtypesstartsuccesspysql_user12023-08-12 16:04:23.000create_dtypesendsuccesspysql_user12023-08-12 16:04:23.000load_dtypesstartsuccesspysql_user22023-08-12 16:04:24.000load_dtypesendsuccesspysql_user22023-08-12 16:04:24.000to_sqlstartsuccesspysql_user32023-08-12 16:04:24.000to_sqlendsuccesspysql_user32023-08-12 16:04:32.000data to_sql sample:user_ididcreated_atlangfavorite_countquote_countreply_countretweet_countviews_countbookmark_count*****173239206297701647162******6346252023-04-15 08:57:34.000en20001771*****04171647153******6135702023-04-15 08:23:18.000en1010120*****495851525652491682090******9745932023-07-20 18:08:18.000en642836521317491788*****873758595686421647152******9572482023-04-15 08:18:00.000ar2280070*****00130283233291647127******0335372023-04-15 06:40:00.000en2010842dtypes table sample:tableschemadtypes_strproccess_idT1Twitter{'user_id': 'types.BIGINT()', ... , 'bookmark_count': 'types.INTEGER()'}1T2Twitter{'user_profile_banner_url': 'types.NVARCHAR(88)',..., 'user_description_urls': 'types.NVARCHAR(443)'}8T3Twitter{'in_reply_to_status_id_str': 'types.BIGINT()',..., 'is_quote': 'types.BOOLEAN()'}11