- Required packages
- Connecting to SQL Server
- Checking databases in SQL Server
- Connecting to a specified database
- Extracting data with SQL query
pyodbc is an open source Python module that makes accessing ODBC databases
simple. It implements the DB API 2.0 specification but is packed with even more
Connecting to SQL Server
Let’s read this example line by line: the first two lines create a connection
to a SQL Server named “sqlprod” using
SQL Server as the connection method;
then we specify usename and password with
Checking databases in SQL Server
After creating the connection to SQL Server, we can check databases in it.
“db” returns rich information of all databases which are contained in SQL Server, like name, database_id, source_database_id, create_date, etc.
Connecting to a specified database
By checking databases in SQL Server, we can now connect to a specified database.
Connecting to a database should specify the database-name in
Extracting data with SQL query
To connect to “TABLE” of database “db1”, we need to precise the connection
“cnxn_db1” when execute SQL query with