XX
XX
import csv
import os
import psycopg2
# Open a new CSV file if the row count exceeds the maximum rows per
file
if row_count >= max_rows_per_file:
# Close the current CSV file
csv_file.close() # Close the file object directly
# Remove the first and last double quotes, if present, from each
value
values = [value.strip('"') for value in values]
# Replace double quotes within values with two double quotes to
escape them
values = [value.replace('"', '""') for value in values]
except Exception as e:
print(f"Failed to insert data into {table_name} table from {csv_file_path}.
Error: {str(e)}")
# Example usage:
input_file = r"D:\Backup\Script MobileDB - Data\dbo.Mobile.Table.sql"
output_folder = r"D:\Backup\CSV"
max_rows_per_file = 1048576 # Example maximum rows per file
db_params = {
'dbname': 'mobiledb',
'user': 'postgres',
'password': 'supostgres',
'host': 'localhost',
'port': '5432'
}
extract_data_to_csv(input_file, output_folder, max_rows_per_file, db_params)