|
| 1 | +from sqlalchemy import create_engine |
| 2 | +import pandas as pd |
| 3 | + |
| 4 | + |
| 5 | +def tableFromSql(server: str, database: str, table_name: str, user: str = "", password: str = "", schema: str = None, index_col: str = None, coerce_float: bool = True, parse_dates: list = None, columns: list = None, chunksize: int = None): |
| 6 | + ''' |
| 7 | + Returns a SQL table in a DataFrame. |
| 8 | + |
| 9 | + Convert a table stored in SQL Server 2016 into a pandas dataframe. |
| 10 | + Uses sqlalchemy and pandas. |
| 11 | +
|
| 12 | + Parameters |
| 13 | + ---------- |
| 14 | + server : string |
| 15 | + Name of the SQL server |
| 16 | + database : string |
| 17 | + Name of the SQL database |
| 18 | + user : string, default: "" |
| 19 | + If verification is required, name of the user |
| 20 | + password : string, default: "" |
| 21 | + If verification is required, password of the user |
| 22 | + table_name : string |
| 23 | + Name of SQL table in database. |
| 24 | + schema : string, default : None |
| 25 | + Name of SQL schema in database to query (if database flavor supports this). Uses |
| 26 | + default schema if None (default). |
| 27 | + index_col : string or list of strings, default : None |
| 28 | + Column(s) to set as index(MultiIndex). |
| 29 | + coerce_float : boolean, default : True |
| 30 | + Attempts to convert values of non-string, non-numeric objects (like decimal.Decimal) |
| 31 | + to floating point. Can result in loss of Precision. |
| 32 | + parse_dates : list or dict, default : None |
| 33 | + - List of column names to parse as dates. |
| 34 | + - Dict of {column_name: format string} where format string is strftime compatible in |
| 35 | + case of parsing string times or is one of (D, s, ns, ms, us) in case of parsing |
| 36 | + integer timestamps. |
| 37 | + - Dict of {column_name: arg dict}, where the arg dict corresponds to the keyword |
| 38 | + arguments of pandas.to_datetime() Especially useful with databases without native |
| 39 | + Datetime support, such as SQLite. |
| 40 | + columns : list, default : None |
| 41 | + List of column names to select from SQL table |
| 42 | + chunksize : int, default : None |
| 43 | + If specified, returns an iterator where chunksize is the number of rows to include |
| 44 | + in each chunk. |
| 45 | + |
| 46 | + Returns |
| 47 | + ---------- |
| 48 | + pd.DataFrame |
| 49 | + Dataframe of the table requested from sql server |
| 50 | +
|
| 51 | + Examples |
| 52 | + --------- |
| 53 | + # >>> tableFromSql("myServer2", "myDatabase2", "myTable2") |
| 54 | + # pd.DataFrame |
| 55 | + # >>> tableFromSql("myServer", "myDatabase", "myTable", schema="specialSchema", columns=["col_1", "col_3"]) |
| 56 | + # pd.DataFrame |
| 57 | + ''' |
| 58 | + |
| 59 | + try: |
| 60 | + uri = "mssql+pyodbc://{}:{}@{}/{}?driver=SQL Server Native Client 11.0".format(user, password, server, database) |
| 61 | + engine = create_engine(uri) |
| 62 | + return pd.read_sql_table(table_name, engine, schema=schema, index_col=index_col, coerce_float=coerce_float, parse_dates=parse_dates, columns=columns, chunksize=chunksize) |
| 63 | + except Exception as error: |
| 64 | + raise error |
| 65 | + |
0 commit comments