was developed as I was working with Sql Servers to make it easy and quick to do a lot of the functionality that would duplicate code for in Data Science my projects. This is not a comprehensive list of functionality but the most popular ones I used. This version does not support Azure at the moment. That will come in a later version.
pip install SqlMethods
Each class instance can only connect to one database. To connect to multiple databases you will need multple instances of the class.
from SqlMethods import SqlMethods list_args = [r'login name', r'server name', r'passwrod', r'database name'] sql_srvr = SqlMethods(list_args) print(sql_svr.bool_is_connected)
True # if connection is established # or False # if connection is not established
# simple query string_query = 'select top 10 * from dbo.table' list_query_results = sql_srvr.query_select(string_query)
list_results_for_series = [x[0] for x in list_query_results[1]] series_one_column = pandas.Series(data = list_results_for_series, name = 'column_name')
list_table_columns = sql_srvr.get_table_columns('table_name') if list_table_columns[0] and list_query_results[0]: df_sql_query = pandas.DataFrame(data = list_query_results[1], columns = list_table_columns[1])
bool_table_exists = sql_srvr.table_exists(string_table_name) if not bool_table_exists: list_create_table = sql_srvr.create_table(string_table_name, ['int_id int', 'date_record datetime', 'string_customer varchar(100)']) bool_table_exists = list_create_table[0]
list_insert = df_sql_query.values.tolist()
if bool_table_exists: list_insert_results = sql_srvr.insert(string_table_name, list_table_columns, df_sql_query.values.tolist())
list_insert_results
the format is the samelist_query_results
. The first value is a boolean determining if the insert completed with noTrue
. If the first value isFalse
then the error message will be in the second postion of the list.sql_srvr.close()