This Python script updates a Microsoft SQL Server database by creating the database and table if they do not exist, and inserting data from a CSV file into the specified table.
- 🔗 Connects to SQL Server using Windows Authentication or SQL Server Authentication
- 🗄️ Automatically creates the database if it doesn't exist
- 📊 Creates tables with columns based on CSV file headers
- 📁 Inserts data from CSV files into the specified table
- 🛡️ Includes error handling and connection management
- Python 3.x
- Microsoft SQL Server (Express, Standard, or Enterprise)
- ODBC Driver 17 for SQL Server
-
Clone this repository:
git clone https://github.com/Aditya-Raj-Parashar/data-entry.git cd database-updater -
Install required dependencies:
pip install -r requirements.txt
-
Ensure ODBC Driver 17 for SQL Server is installed on your machine:
- Download from Microsoft's official page
Before running the script, update the following variables in database_updater.py:
server = 'localhost' # Your SQL Server instance
database = 'your_database_name' # Database name
table_name = 'dbo.your_table_name' # Table name
csv_file_path = 'path/to/your/file.csv' # CSV file path or for testing you can use the given csv files in the repository data entry\Bank+Customer+Churn
## Usage
### Windows Authentication (Default)
```bash
python database_updater.pyIf you need to use SQL Server authentication instead of Windows authentication, modify the connection string in the script:
# Replace the existing connection_string with:
connection_string = f"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={server};DATABASE={database};UID=your_username;PWD=your_password;"- CSV file should have headers in the first row
- All data will be stored as NVARCHAR(255) in the database
- Ensure your CSV file doesn't contain sensitive information before committing to version control
The script includes comprehensive error handling for:
- Database connection issues
- File not found errors
- SQL execution errors
- Data type conversion issues
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Never commit CSV files containing sensitive data
- Use environment variables for database credentials in production
- Consider using connection pooling for high-volume operations
- ODBC Driver not found: Ensure ODBC Driver 17 for SQL Server is installed
- Connection refused: Check if SQL Server is running and accessible
- Authentication failed: Verify Windows Authentication is enabled or use correct SQL Server credentials
- Permission denied: Ensure the user has appropriate database creation/modification permissions
If you encounter issues:
- Check the error message in the console output
- Verify your SQL Server configuration
- Ensure all dependencies are installed correctly
- Open an issue in this repository with details about the problem
- Initial release
- Basic CSV to SQL Server functionality
- Windows and SQL Server authentication support
- Automatic database and table creation
This project is open scourced