Write data to SQL database using Python

To write data to a SQL database using Python, you can use the pyodbc library to connect to the database and insert the data. Here’s a sample code.

import pyodbc

# Connect to the SQL database
conn = pyodbc.connect('Driver={SQL Server};Server=server_name;Database=database_name;Trusted_Connection=yes;')

# Define the data to insert into the database
data = [
    ('John', 'Doe', 25),
    ('Jane', 'Doe', 30),
    ('Bob', 'Smith', 40),
]

# Iterate through the data and insert it into the database
for row in data:
    cursor = conn.cursor()
    cursor.execute("INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?)", row[0], row[1], row[2])
    cursor.commit()

# Close the database connection
conn.close()

In this example, the data is defined as a list of tuples, where each tuple represents a row of data to be inserted into the table table_name in the SQL database. You can modify the SQL query in the cursor.execute() method to match the columns and table structure in your database.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech