MSSQL.create_connection
- MSSQL.create_connection(database_name=None, mode=None)[source]
Create a SQLAlchemy connection.
- Parameters:
database_name (str | None) – name of a database, defaults to the name of the currently-connected database when
database=None
mode (None | str) – when
mode=None
(default), the method uses the existing engine; whenmode='pyodbc'
(optional), it uses pyodbc.connect()
- Returns:
a SQLAlchemy connection to a Microsoft SQL Server
- Return type:
sqlalchemy.engine.Connection | pyodbc.Connection
Examples:
>>> from pyhelpers.dbms import MSSQL >>> import sqlalchemy >>> mssql = MSSQL() Connecting <server_name>@localhost:1433/master ... Successfully. >>> db_conn = mssql.create_connection() >>> db_conn.should_close_with_result False >>> db_conn.closed False >>> res = db_conn.execute(sqlalchemy.text('SELECT 1')) >>> res.fetchall() [(1,)] >>> db_conn.closed False >>> db_conn.close() >>> db_conn.closed True