def begin_import(): csv_data = csv.reader(codecs.open(csv_file, 'rb', 'utf-16'), delimiter='\t', quotechar='|') column_names = next(csv_data, None) try: session = Session() for row in csv_data: row.append(current_platform) session_row = ChartboostCampaignAppMetric() session_row.Date = datetime.datetime.strptime(row[0].strip('"'), "%Y-%m-%d").date() session_row.ToCampaignName = row[1] session_row.ToCampaignID = row[2] session_row.ToAppName = row[3] session_row.ToAppID = row[4] session_row.ToAppBundle = row[5] session_row.FromCampaignName = row[6] session_row.FromCampaignID = row[7] session_row.FromAppName = row[8] session_row.FromAppID = row[9] session_row.FromAppBundle = row[10] session_row.CampaignType = row[11] session_row.Role = row[12] session_row.AdType = row[13] session_row.Impressions = try_parse_int(row[14]) session_row.Clicks = try_parse_int(row[15]) session_row.Installs = try_parse_int(row[16]) session_row.CTR = try_parse_float(row[17]) session_row.IR = try_parse_float(row[18]) session_row.MoneyEarned = try_parse_float(row[19]) session_row.eCPMEarned = try_parse_float(row[20]) session_row.MoneySpent = try_parse_float(row[21]) session_row.eCPMSpent = try_parse_float(row[22]) session_row.CompletedView = try_parse_int(row[23]) session_row.Platform = row[24] try: session.merge(session_row) except SQLAlchemyError as e: log.exception("Failed to merge %s." % (e)) session.commit() except: session.rollback() session.close() raise finally: session.close() class ChartboostCampaignAppMetric(Base): __tablename__ = 'ChartboostCampaignByApps' Date = Column(DateTime, primary_key=True) ToCampaignName = Column(String(100)) ToCampaignID = Column(String(100), primary_key=True) ToAppName = Column(String(100)) ToAppID = Column(String(100),primary_key=True) ToAppBundle = Column(String(100)) FromCampaignName = Column(String(100)) FromCampaignID = Column(String(100),primary_key=True) FromAppName = Column(String(100)) FromAppID = Column(String(100),primary_key=True) FromAppBundle = Column(String(100)) CampaignType = Column(String(100),primary_key=True) Role = Column(String(100),primary_key=True) AdType = Column(String(100),primary_key=True) Impressions = Column(BigInteger(),default = 0) Clicks = Column(BigInteger(),default = 0) Installs = Column(BigInteger(),default = 0) CTR = Column(Numeric(18, 2),default = 0) IR = Column(Numeric(18, 2),default = 0) MoneyEarned = Column(Numeric(18, 2),default = 0) eCPMEarned = Column(Numeric(18, 2),default = 0) MoneySpent = Column(Numeric(18, 2),default = 0) eCPMSpent = Column(Numeric(18, 2),default = 0) CompletedView = Column(BigInteger(),default = 0) Platform = Column(String(100),primary_key=True)