{"id":15286474,"url":"https://github.com/carvetighter/sqlmethods","last_synced_at":"2026-01-04T20:46:43.983Z","repository":{"id":57470604,"uuid":"122760136","full_name":"carvetighter/SqlMethods","owner":"carvetighter","description":"A wrapper for pymssql (Microsoft Sql Server Package).  I wrote this in the process of working with Sql Servers as a Data Scientist.","archived":false,"fork":false,"pushed_at":"2019-08-23T22:05:29.000Z","size":83,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-13T05:20:25.108Z","etag":null,"topics":["microsoft-sql","microsoft-sql-server","pymssql","python","python-3","python3","sql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/carvetighter.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-24T16:57:55.000Z","updated_at":"2021-12-08T00:59:04.000Z","dependencies_parsed_at":"2022-09-26T17:40:34.345Z","dependency_job_id":null,"html_url":"https://github.com/carvetighter/SqlMethods","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carvetighter%2FSqlMethods","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carvetighter%2FSqlMethods/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carvetighter%2FSqlMethods/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carvetighter%2FSqlMethods/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/carvetighter","download_url":"https://codeload.github.com/carvetighter/SqlMethods/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245170115,"owners_count":20572007,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["microsoft-sql","microsoft-sql-server","pymssql","python","python-3","python3","sql"],"created_at":"2024-09-30T15:14:32.108Z","updated_at":"2026-01-04T20:46:43.927Z","avatar_url":"https://github.com/carvetighter.png","language":"Python","readme":"SqlMethods\n-------------------------\n\n| This package is disigned to be a light wight wrapper for pymssql used for Miscrosoft SQL Server.  This\nwas developed as I was working with Sql Servers to make it easy and quick to do a lot of the \nfunctionality that would duplicate code for in Data Science my projects.  This is not a comprehensive \nlist of functionality but the most popular ones I used.  This version does not support Azure at the \nmoment.  That will come in a later version.\n\nInstallation\n============\n``pip install SqlMethods``\n\nUsage\n=====\n| **Connect to the SQL Server:**\n\nEach class instance can only connect to one database.  To connect to multiple databases you will\nneed multple instances of the class.\n\n::\n\n    from SqlMethods import SqlMethods\n    list_args = [r'login name', r'server name', r'passwrod', r'database name']\n\n    sql_srvr = SqlMethods(list_args)\n    print(sql_svr.bool_is_connected)\n\n| **Connect to the Sql Server ouput**:\n\n::\n\n    True # if connection is established\n    # or\n    False # if connection is not established\n\n| **Query database:**\n| Assuming a connection (sql_srvr) is established.\n\n::\n\n    # simple query\n    string_query = 'select top 10 * from dbo.table'\n    list_query_results = sql_srvr.query_select(string_query)\n\n| The list returned is a list of length 2.\n|\n| list_query_results[0] -\u003e boolean; True or False\n| list_query_results[1] -\u003e potentially a multidimensional list of values from the query; in this case the first 10 records\n|   if list_query_results[0] is True list_query_results[1] will have the data\n|   if list_query_results[0] is False list_query_results[1] will have the error message from the Sql Server\n|\n| Note: if you are pulling only one column or your results results in a sinlge column of data.  You will\n| need to use a list comprehension to be able to add it to a pandas Series.\n| \n| example:\n::\n\n    list_results_for_series = [x[0] for x in list_query_results[1]]\n    series_one_column = pandas.Series(data = list_results_for_series, name = 'column_name')\n\n| If your query reults in more than one column of data you can use the following code to add it to\n| a pandas datafarme.\n|\n| example:\n::\n\n    list_table_columns = sql_srvr.get_table_columns('table_name')\n    if list_table_columns[0] and list_query_results[0]:\n        df_sql_query = pandas.DataFrame(data = list_query_results[1], columns = list_table_columns[1])\n\n| **Insert into database:**\n| Before you insert it is a good idea to check if the table is created.  The below example illistrates how\n| to insert into the table and check if the table is created.\n|\n| example (check table or create):\n::\n\n    bool_table_exists = sql_srvr.table_exists(string_table_name)\n    if not bool_table_exists:\n        list_create_table = sql_srvr.create_table(string_table_name, \n                                ['int_id int', 'date_record datetime', 'string_customer varchar(100)'])\n        bool_table_exists = list_create_table[0]\n    \n| The columns are in a list with the column name and data type.  The when the table is created\n| the data compression is enable and the table is a narrow table (1,024 columns or less).\n|\n| When inserting into the table the method will determine if you are inerting one value or more than\n| by the lenght of the list of values.  This needs to be a list of lists.  An easy way to convert a pandas\n| dataframe is to use the following line of code:\n::\n    \n    list_insert = df_sql_query.values.tolist()\n\n| The following example will insert into the table created above:\n::\n\n    if bool_table_exists:\n        list_insert_results = sql_srvr.insert(string_table_name, list_table_columns, \n                                df_sql_query.values.tolist())\n\n| The method will insert 100,000 records at a time.  In ``list_insert_results`` the format is the same\n| as ``list_query_results``.  The first value is a boolean determining if the insert completed with no\n| errors.  The second values will be an empy list of the first value is ``True``.  If the first value is \n| ``False`` then the error message will be in the second postion of the list.\n|\n| **Other methods:**\n| Other functions in the package are:\n|\n| delete_table() -\u003e deletes a table in the database\n| truncate_table() -\u003e clears the table in the database\n| update() -\u003e changes values in the table based on specific criteria\n| delete_records() -\u003e removes records from the directory\n| get_wide_columns() -\u003e finds the columns of a wide / sparse table\n| get_num_columns() -\u003e returns the number of columns in a table\n| alter_table() -\u003e changes the columns (add, delete, modify) in a table\n| \n| **Close connection:**\n| The last and also important aspect of the table is to close the connection.\n| \n| example:\n|\n    ``sql_srvr.close()``","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarvetighter%2Fsqlmethods","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcarvetighter%2Fsqlmethods","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarvetighter%2Fsqlmethods/lists"}