In [ ]:
import pyodbc
# Connecting to MSSQL
conn = pyodbc.connect(
''''DRIVER={ODBC Driver 17 for SQL Server};''''
''''SERVER=your_server;''''
''''DATABASE=your_database;''''
''''UID=your_username;''''
''''PWD=your_password''''
)
cursor = conn.cursor()
# Creating a table
cursor.execute(''''''''''''
CREATE TABLE users (
id INT PRIMARY KEY,
name NVARCHAR(50),
age INT
)
'''''''''''')
# Inserting data
cursor.execute("INSERT INTO users (id, name, age) VALUES (1, ''''Alice'''', 25)")
conn.commit()
# Querying data
cursor.execute("SELECT * FROM users")
for row in cursor:
print(row)
conn.close()