Untitled
import pandas as pd import numpy as np import pyodbc # Generate sample data text1 = [] text2 = [] numbers = [] binary = [] for i in range(200): text1.append("Text1_" + str(i)) text2.append("Text2_" + str(i)) numbers.append(i) binary.append(bool(np.random.choice([True, False]))) # Convert numpy.bool_ to Python bool data = { "TextColumn1": text1, "TextColumn2": text2, "NumericColumn": numbers, "BinaryColumn": binary } df = pd.DataFrame(data) # Save data to CSV (optional) df.to_csv("data.csv", index=False) # Database connection details server = "LAB322PC15\SQLEXPRESS" database = "TemoDataBase" # Use Windows Authentication conn = pyodbc.connect( f"DRIVER={{SQL Server}};SERVER={server};DATABASE={database};Trusted_Connection=yes;" ) cursor = conn.cursor() # Insert data into the table for i in range(200): cursor.execute(""" INSERT INTO DataTable (TextColumn1, TextColumn2, NumericColumn, BinaryColumn) VALUES (?, ?, ?, ?) """, text1[i], text2[i], numbers[i], binary[i]) conn.commit() # Close the connection cursor.close() conn.close() print("Data inserted successfully!")
Leave a Comment