0% found this document useful (0 votes)
2 views2 pages

Notebook Modulo 2 Python PDF

The document outlines the process of connecting to a SQL Server database using Python's pyodbc and pandas libraries. It includes configuration details for the connection, executing a SQL query to retrieve customer data from New York, and saving the results to an Excel file. The final output confirms that there are 1,019 customers from New York in the retrieved data.

Uploaded by

Wilkin JR
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Notebook Modulo 2 Python PDF

The document outlines the process of connecting to a SQL Server database using Python's pyodbc and pandas libraries. It includes configuration details for the connection, executing a SQL query to retrieve customer data from New York, and saving the results to an Excel file. The final output confirms that there are 1,019 customers from New York in the retrieved data.

Uploaded by

Wilkin JR
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Librerias necesarias

In [19]: import pyodbc


import pandas as pd

Configuracion de conexion
server = 'Nombre del servidor'

database = 'Nombre de la Base de datos'

username = 'Nombre de Usuario'

password = 'Contraseña del Usuario'

In [20]: server = r'(localdb)\PRACTICAS'


database = 'BikeStores2'
username = 'WilkinJR'
password = 'w54321'

In [21]: conn_str = (
r'DRIVER={ODBC Driver 17 for SQL Server};'
rf'SERVER={server};DATABASE={database};'
rf'UID={username};PWD={password};'
r'Encrypt=no;TrustServerCertificate=yes;'
)

Probar conexión
In [22]: try:


conn = [Link](conn_str)
print(" Conexión establecida correctamente con SQL Server.")


except Exception as e:
print(" Error al conectar:", e)

✅ Conexión establecida correctamente con SQL Server.


Ejecutar la consulta
In [23]: query = """
SELECT
customer_id,
first_name,
last_name,
phone,
email,
street,
city,
state,
zip_code
FROM [Link]
WHERE state = 'NY';
"""

ClientesNY = pd.read_sql_query(query, conn)


[Link]()

# Mostrar el DataFrame
[Link]()

C:\Users\NeoXG\AppData\Local\Temp\ipykernel_18184\[Link]: UserWarning: pandas only supports SQLAlchemy connectable (engine/connection) or database string URI or sqlite3 DBAPI2 conne
ction. Other DBAPI2 objects are not tested. Please consider using SQLAlchemy.
ClientesNY = pd.read_sql_query(query, conn)
Out[23]: customer_id first_name last_name phone email street city state zip_code

0 1 Debra Burks None [Link]@[Link] 9273 Thorne Ave. Orchard Park NY 14127

1 4 Daryl Spence None [Link]@[Link] 988 Pearl Lane Uniondale NY 11553

2 6 Lyndsey Bean None [Link]@[Link] 769 West Road Fairport NY 14450

3 7 Latasha Hays (716) 986-3359 [Link]@[Link] 7014 Manor Station Rd. Buffalo NY 14215

4 8 Jacquline Duncan None [Link]@[Link] 15 Brown St. Jackson Heights NY 11372

Guardar el resultado
In [25]: ClientesNY.to_excel("[Link]", index=False)

Confirmar el número de registros


In [27]: print("Cantidad de clientes del estado de NY:", len(ClientesNY))

Cantidad de clientes del estado de NY: 1019

In [ ]:

You might also like