Untitled
unknown
plain_text
10 months ago
1.5 kB
9
Indexable
import sqlite3
import pandas as pd
# Define database file
db_file = "your_database.db"
# Define CSV file paths
attr_csv = "attr_data.csv"
logic_csv = "logic_data.csv"
# Connect to SQLite database
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
# Function to count rows in a table
def count_rows_in_table(table_name, conn):
cursor = conn.cursor()
cursor.execute(f"SELECT COUNT(*) FROM {table_name}")
return cursor.fetchone()[0]
# Function to count rows in a CSV file
def count_rows_in_csv(csv_file):
df = pd.read_csv(csv_file)
return len(df)
# Function to insert CSV into a table
def insert_csv_to_table(csv_file, table_name, conn):
df = pd.read_csv(csv_file) # Load CSV into a DataFrame
df.to_sql(table_name, conn, if_exists='append', index=False) # Insert into table
# Process both tables
for table, csv_file in [("Attr", attr_csv), ("Logic", logic_csv)]:
# Count before appending
db_rows_before = count_rows_in_table(table, conn)
csv_rows = count_rows_in_csv(csv_file)
print(f"Table: {table}")
print(f" - Rows in DB before appending: {db_rows_before}")
print(f" - Rows in CSV file: {csv_rows}")
# Insert CSV data into the table
insert_csv_to_table(csv_file, table, conn)
# Count after appending
db_rows_after = count_rows_in_table(table, conn)
print(f" - Rows in DB after appending: {db_rows_after}\n")
# Commit and close connection
conn.commit()
conn.close()
Editor is loading...
Leave a Comment