Skip to content

Conversation

@princexpoddar
Copy link
Contributor

@princexpoddar princexpoddar commented Oct 27, 2025

Fixes #2

Summary

Transformed the basic SQLite script into a robust, idempotent database setup utility with CLI interface and comprehensive error handling.

Key Changes

✅ Idempotent Execution

  • Wrapped execution in if __name__ == "__main__" block
  • Added CREATE TABLE IF NOT EXISTS to prevent table creation errors
  • Implemented INSERT OR IGNORE with unique constraints to prevent duplicate records

✅ CLI Interface

  • Added argparse for command-line argument handling
  • --db-path: Specify custom database file path (default: student.db)
  • --mode: Choose between append (default) or recreate modes
  • --no-display: Skip record display after setup

✅ Data Integrity

  • Added unique constraint on (NAME, CLASS, SECTION) to prevent duplicates
  • Implemented auto-increment ID as primary key
  • Used parameterized queries to prevent SQL injection

✅ Error Handling & Robustness

  • Comprehensive try-catch blocks with proper rollback on errors
  • Type hints for better code maintainability
  • Windows-compatible output (fixed Unicode character issues)

✅ Testing Verified

  • Script runs multiple times without errors or duplicates
  • Both append and recreate modes work correctly
  • All CLI flags function as expected

Usage Examples

# Basic usage (append mode)
py sqlite.py

# Recreate mode (clears existing data)
py sqlite.py --mode recreate

# Custom database path
py sqlite.py --db-path my_database.db

# Skip displaying records
py sqlite.py --no-display

# Show help
py sqlite.py --help

Benefits

  • Safe to run multiple times without side effects
  • Flexible CLI interface for different use cases
  • Production-ready error handling and logging
  • Maintains backward compatibility with existing data

Files Changed

  • sqlite.py - Complete refactor with idempotent functionality

Testing

  • ✅ No linter errors
  • ✅ Idempotent execution verified
  • ✅ CLI flags tested
  • ✅ Error handling validated
  • ✅ Data integrity confirmed

Before vs After

Before

# Basic script that would fail on multiple runs
connection = sqlite3.connect("student.db")
cursor = connection.cursor()
cursor.execute("create table STUDENT(...)")
cursor.execute("Insert Into STUDENT values(...)")

After

# Idempotent script with CLI interface
if __name__ == "__main__":
    main()

def main():
    parser = argparse.ArgumentParser(...)
    # Robust error handling and idempotent operations

- Add if __name__ == "__main__" wrapper
- Use CREATE TABLE IF NOT EXISTS and INSERT OR IGNORE
- Add CLI flags for db-path, mode (append/recreate), no-display
- Add unique constraints and error handling
- Enable safe multiple executions
@Neha20072002
Copy link
Owner

@princexpoddar link your PR to concerned issue

@princexpoddar
Copy link
Contributor Author

@Neha20072002 I have resolved the issue..kindly see

@Neha20072002
Copy link
Owner

Accepted

@Neha20072002 Neha20072002 merged commit d49adea into Neha20072002:main Oct 27, 2025
0 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert sqlite.py into an idempotent DB setup script

2 participants