File size: 2,770 Bytes
7fd4ede | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | import sqlite3
import logging
from typing import Dict, Any, Optional
logger = logging.getLogger(__name__)
class DatabaseManager:
def __init__(self, db_path: str = "leads.db"):
self.db_path = db_path
self._init_db()
def _init_db(self):
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS leads (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_name TEXT NOT NULL,
industry_segment TEXT,
city TEXT NOT NULL,
market_priority TEXT,
contact_email TEXT NOT NULL,
website_url TEXT,
source_platform TEXT,
generated_pitch TEXT,
status TEXT DEFAULT 'PENDING_REVIEW'
)
''')
conn.commit()
logger.info(f"Database initialized at {self.db_path}")
except sqlite3.Error as e:
logger.error(f"Failed to initialize database: {e}")
raise
def insert_lead(self, lead_data: Dict[str, Any]) -> Optional[int]:
try:
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO leads (
company_name, industry_segment, city, market_priority,
contact_email, website_url, source_platform, generated_pitch, status
) VALUES (
:company_name, :industry_segment, :city, :market_priority,
:contact_email, :website_url, :source_platform, :generated_pitch, :status
)
''', {
'company_name': lead_data.get('company_name'),
'industry_segment': lead_data.get('industry_segment'),
'city': lead_data.get('city'),
'market_priority': lead_data.get('market_priority', 'National'),
'contact_email': lead_data.get('contact_email'),
'website_url': lead_data.get('website_url'),
'source_platform': lead_data.get('source_platform'),
'generated_pitch': lead_data.get('generated_pitch'),
'status': lead_data.get('status', 'PENDING_REVIEW')
})
conn.commit()
return cursor.lastrowid
except sqlite3.Error as e:
logger.error(f"Failed to insert lead {lead_data.get('company_name')}: {e}")
return None
|