import pyodbc
# Set up the database connection
conn = pyodbc.connect('DRIVER={DriverName};SERVER=server_name;DATABASE=database_name;UID=username;PWD=password')
# Create a cursor object to execute SQL commands
cursor = conn.cursor()
# Define the SQL script to test
sql_script = '''
CREATE TABLE test_table (
id INT PRIMARY KEY,
name VARCHAR(50)
);
INSERT INTO test_table (id, name) VALUES (1, 'John');
INSERT INTO test_table (id, name) VALUES (2, 'Jane');
'''
try:
# Execute the SQL script
cursor.execute(sql_script)
conn.commit()
# Perform any necessary tests, e.g., fetch results
cursor.execute('SELECT * FROM test_table')
results = cursor.fetchall()
for row in results:
print(row)
except pyodbc.Error as ex:
print('Error executing SQL script:', ex)
# Close the cursor and connection
cursor.close()
conn.close()