MSSQL.connect_database

MSSQL.connect_database(database_name=None, verbose=False)[source]

Establish a connection to a database.

Parameters:
  • database_name (str | None) – Name of the database to connect to; if database_name=None (default), the database name is input manually.

  • verbose (bool | int) – Whether to print relevant information to the console; defaults to False.

Examples:

>>> from pyhelpers.dbms import MSSQL
>>> testdb = MSSQL(database_name='testdb')
Creating a database: [testdb] ... Done.
Connecting <server_name>@localhost:1433/testdb ... Successfully.
>>> testdb.connect_database(verbose=True)
Being connected with <server_name>@localhost:1433/testdb.
>>> testdb.connect_database(database_name='master', verbose=True)
Connecting <server_name>@localhost:1433/master ... Successfully.
>>> testdb.database_name
'master'
>>> testdb.connect_database(database_name='testdb', verbose=True)
Connecting <server_name>@localhost:1433/testdb ... Successfully.
>>> testdb.database_name
'testdb'
>>> testdb.drop_database(verbose=True)  # Delete the database [testdb]
To drop the database [testdb] from <server_name>@localhost:1433
? [No]|Yes: yes
Dropping [testdb] ... Done.
>>> testdb.database_name
'master'