This Python program reads financial transactions from a CSV file and loads them into a MySQL database. Here’s what it does step-by-step:
- Uses the
mysql.connectorlibrary to connect to a MySQL database server onlocalhost. - Uses the
rootuser and reads the password from the environment variableMYSQL_PASS. - Connects to a database named
finances.
- Executes a SQL statement to create the table
pocketsmith_searchif it doesn’t already exist. - The table schema matches columns in the CSV:
- Includes fields for dates, merchant name, amounts, currency, account details, transaction type, and several descriptive fields.
- Opens the file
pocketsmith-search.csvfor reading. - Skips the header row and reads each transaction row.
- Converts any empty string fields in each row to
None(allowing them to be stored as SQLNULL). - For each row, executes an
INSERT INTO pocketsmith_search VALUES ...SQL statement to add the record to the database.
- After all rows are processed, commits the transaction to MySQL to save changes.
- Closes the cursor and the connection to clean up resources.
- Purpose: Quickly and reliably import CSV financial transaction data into a MySQL table.
- Automation: Handles table creation, data type matching, empty field conversion, and commits all changes without manual intervention.
- Usage: Useful for finance or accounting applications needing bulk imports, ETL processes, or database population for analytics from CSV files.