Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8516243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:22:15+00:00 2026-06-11T05:22:15+00:00

I am working on a script that is going to back-fill Call Detail Records

  • 0

I am working on a script that is going to back-fill Call Detail Records from our asterisk system into our MySQL logs database. In the code below, I am trying to ignore the duplicate keys and continue onto the next row, however when this code executes all I ever see is the first row dupe warning then the script exits (example 2, below.) Is there a an obvious error that’s causing it to fail?

Forgive my poor python etiquette, I’m very new to the language. My assumption is that even though pass might break out of an exception, the foreach loop doesn’t survive.

EDIT/NOTE: This is also worth mentioning after I solved the problem of the exiting for loop: because I had a finally: block which closed the sql connection, the finally: block was being executed after the except: pass and closing the connection. So in the above example, the finally: was still causing the program to abend.

#!/usr/bin/python -d

import csv
import sys
import MySQLdb as mdb
log="Master.csv"

try:
        con = mdb.connect('1.2.3.4','abcd','efgh','ijkl')
        cur = con.cursor()

        #Inefficient way of getting row count.
        rcount = csv.reader(open(log, 'rb'))
        print "Number of rows in csv: %d" % (len(list(rcount)))

        #OK, real csv processing now.
        reader = csv.reader(open(log, 'rb'))

        iter = 0
        for row in reader:
                print "Row: %d" % (++iter)

                clid = row[0]
                src = row[1]
                dst = row[2]
                dcontext = row[3]
                channel = row[4]
                dstchannel = row[5]
                lastapp = row[6]
                lastdata = row[7]
                start = row[8]
                end = row[10]
                duration = row[11]
                billsec = row[12]
                disposition = row[13]
                amaflags = row[14]
                accountcode = row[15]
                uniqueid = row[16]

                insertstr= "INSERT INTO cdr_extended (duration,billsec,amaflags,start,end,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,disposition,accountcode,uniqueid) VALUES (%s,%s,0,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (duration,billsec,start,end,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,disposition,accountcode,uniqueid)
                cur.execute(insertstr)
                con.commit()

except mdb.Error, e:
        if e.args[0] == 1062:
                print "Dupe key on uniqueid: %s"  % (uniqueid)
                pass
        else:
                print "Error %d: %s" % (e.args[0],e.args[1])
                sys.exit(1)

finally:
        if con:
                con.close()

The output:

Number of rows in csv: 2696
Row: 0
Dupe key on uniqueid: 1342632723.8
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:22:16+00:00Added an answer on June 11, 2026 at 5:22 am

    Wrap your try...except block only around the code that would cause the exception, which is in the for loop. I don’t use MySQLdb, but something like this should work:

                try:
                    insertstr= "INSERT INTO cdr_extended (duration,billsec,amaflags,start,end,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,disposition,accountcode,uniqueid) VALUES (%s,%s,0,'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (duration,billsec,start,end,clid,src,dst,dcontext,channel,dstchannel,lastapp,lastdata,disposition,accountcode,uniqueid)
                    cur.execute(insertstr)
                    con.commit()
                except mdb.Error, e:
                    if e.args[0] == 1062:
                        print "Dupe key on uniqueid: %s"  % (uniqueid)
                    else:
                        print "Error %d: %s" % (e.args[0],e.args[1])
                        sys.exit(1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im working on a script that will pull a variable from my MySQL db
i'm working on a script that will monitor traffic on specific hosts from nagios.
I'm working on a script that is supposed to install some software from a
I was working on a script that grabs the value from the selected option
I'm working on a script that takes the elements from companies and pairs them
Im working on a script that needs to find a match for certain letters(chords)
I'm working with a script that is calling the same class twice and throwing
I'm working on a script that will check if a user is an Administrator
I am working on a script that will scroll through list elements by clicking
I am working on php script that may involve a long running task. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.