Como Conectar o Python Ao SQL Server Usando Pyodbc - Data To Fish
Como Conectar o Python Ao SQL Server Usando Pyodbc - Data To Fish
Nesse caso, mostrarei as etapas para estabelecer esse tipo de conexão usando um exemplo
simples.
Jade 20 Londres
Martin 25 Londres
Roubar 35 Genebra
https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/ 1/8
7/25/2020 Como conectar o Python ao SQL Server usando pyodbc - Data to Fish
First, you’ll need to install the pyodbc package which will be used to connect Python to SQL
Server.
You can use the PIP install method to install the pyodbc package:
https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/ 2/8
7/25/2020 Como conectar o Python ao SQL Server usando pyodbc - Data to Fish
One way to find your current server name is by running the following query:
SELECT @@SERVERNAME
Next, obtain the database name in which your desired table is stored.
You can find the database name under the Object Explorer menu (underneath the Databases
section), which is located on the left side of your SQL Server.
The name of your table would also be located under the Object Explorer menu (underneath the
Tables section).
https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/ 3/8
7/25/2020 Como conectar o Python ao SQL Server usando pyodbc - Data to Fish
The following data will be displayed in SQL Server when you run a simple SELECT query using
the dbo.Person table. This is also the data that you’ll get once you connect Python to SQL
Server using pyodbc.
And for the final part, open your Python IDLE and fill the server name, database and table
information.
Here is the structure of the code that you may use in Python:
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/ 4/8
7/25/2020 Como conectar o Python ao SQL Server usando pyodbc - Data to Fish
'Server=server_name;'
'Database=database_name;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('SELECT * FROM database_name.table')
And this is how the code would look like in Python for our example:
Run the code in Python (adjusted to your server name, database and table information).
You’ll notice that the results that were printed in Python match with the info that was displayed in
SQL Server:
https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/ 5/8
7/25/2020 Como conectar o Python ao SQL Server usando pyodbc - Data to Fish
You can take things further by going from SQL to Pandas DataFrame using pd.read_sql_query:
import pandas as pd
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=RON\SQLEXPRESS;'
'Database=TestDB;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
When applying pd.read_sql_query, don’t forget to place the connection string variable at the
end. In our case, the connection string variable is conn.
Once you run the code (adjusted to your database connection information), you’ll get the
following Pandas DataFrame:
Note that the syntax of print(type(sql_query)) was also added to the code to confirm that now
we’ve got a DataFrame.
https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/ 6/8
7/25/2020 Como conectar o Python ao SQL Server usando pyodbc - Data to Fish
Você também pode usar o Python para inserir valores na tabela do SQL Server .
Se você quiser saber mais sobre os diferentes tipos de conexões entre o Python e outros
aplicativos de banco de dados, verifique os seguintes tutoriais:
Tutoriais
Tutoriais em Python
R Tutorials
Julia Tutorials
Tutoriais do MS Access
Tutoriais do Excel
https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/ 7/8
7/25/2020 Como conectar o Python ao SQL Server usando pyodbc - Data to Fish
Política de Privacidade
Termos de serviço
https://datatofish.com/how-to-connect-python-to-sql-server-using-pyodbc/ 8/8