KEMBAR78
Como Conectar Python A SQL Server Usando PYODBC | PDF | Computers
0% found this document useful (0 votes)
61 views4 pages

Como Conectar Python A SQL Server Usando PYODBC

This document provides 4 Python code examples for connecting to and querying a SQL Server database using PyODBC. The examples show: 1) Connecting to the database and selecting all columns and rows from the Products table 2) Selecting a single column from the Products table 3) Querying for employees with a birthday in the current month 4) Selecting products with zero units in stock

Uploaded by

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

Como Conectar Python A SQL Server Usando PYODBC

This document provides 4 Python code examples for connecting to and querying a SQL Server database using PyODBC. The examples show: 1) Connecting to the database and selecting all columns and rows from the Products table 2) Selecting a single column from the Products table 3) Querying for employees with a birthday in the current month 4) Selecting products with zero units in stock

Uploaded by

Angela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Como conectar Python a SQL Server usando PYODBC

C:\Users\ANGELA>cd\
C:\> cd C:\Python\Scripts
C:\Python\Scripts>pip install pyodbc

2. Consulta de una tabla en Python


import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=LAPTOP-V776FAES\SQLEXPRESS;'
'Database=Northwind;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('SELECT* FROM Northwind.dbo.Products')
for row in cursor:
print(row)
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=LAPTOP-V776FAES\SQLEXPRESS;'
'Database=Northwind;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('SELECT ProductName FROM Northwind.dbo.Products')
for row in cursor:
print(row)

3. Consulta de persona que cumple años este mes en Python

import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=LAPTOP-V776FAES\SQLEXPRESS;'
'Database=Northwind;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('select LastName, FirstName, BirthDate, MONTH(BirthDate) as
Birthday from Northwind.dbo.Employees where
MONTH(BirthDate)=MONTH(GETDATE())')
for row in cursor:
print(row)
4. Muestra los productos que tengan stock cero en Python

import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
'Server=LAPTOP-V776FAES\SQLEXPRESS;'
'Database=Northwind;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('Select ProductName,UnitsInStock from Northwind.dbo.Products
WHERE UnitsInStock =0')
for row in cursor:
print(row)

You might also like