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 7515263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:45:02+00:00 2026-05-30T00:45:02+00:00

I’m doing a data format conversion from Program A’s CSV format to Program B’s

  • 0

I’m doing a data format conversion from Program A’s CSV format to Program B’s CSV format.

Program A’s format looks like:

Fruit,  Orange, $1.99
Fruit,  Apple,  $2.99
Fruit,  Pear,   $5.99
Colour, Red,    #FF0000
Colour, Green,  #00FF00
Colour, Blue,   #0000FF
Colour, Orange, #FF8800

I’ve converted Program A’s CSV file into an SQLite database containing multiple tables, one for each type of record in the original CSV file. Here that would be Fruit and Colour. This maps well onto Program B’s file format, which uses similar tables (with a few funky conversions required.)

Each table in Program A’s CSV file has an ID primary key which is not required to be unique. That is, there can be a “Fruit” row with ID “Orange” and also a “Colour” row with ID “Orange“.

However Program B would consider this an error – it requires the ID primary key to be unique across ALL tables.

What is an efficient way to detect non-unique ID keys, either in the original file (formatted like above) or in the SQL database? There are dozens of tables and thousands of records.

My current approach is similar to (Python code):

import sqlite3, collections
db_conn = sqlite3.connect('db.sqlite3')
db_conn.row_factory = sqlite3.Row

IDs = []    # build a list of (ID, table_name) pairs

tables = ['Fruit','Colour'];
for table in tables:
    rows = db_conn.execute("SELECT ID FROM %s" % table)
    for row in rows:
        IDs.append( (row['ID'],table) )

id_counts = collections.Counter([x(0) for x in IDs])
duplicated_ids = [x for x in id_counts if id_counts[x] > 1]
for duplicated_id in duplicated_ids:    
    duplicated_types = [x(1) for x in IDs if x(0) == duplicated_id ]
    print ("Duplicate ID %(duplicated_id)s used for %(duplicated_types)s" % locals())

This seems hilariously inefficient. There must exist a more elegant/more Pythonic way of doing this, possibly using some kind of set notation.

Alternately, could I define my SQL schema (SQLite flavoured) so that SQLite enforces uniqueness of the ID across all tables?

  • 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-05-30T00:45:03+00:00Added an answer on May 30, 2026 at 12:45 am

    There is no way that I know of to enforce this within the database, unless you create another table with all IDs, keep this up to date using triggers. I’m not sure you ever want to do something like that (whether it’s possible at all depends on your database engine).

    A more efficient way to check for duplicates is using a Hash in stead of an array:

    IDs = {}
    
    tables = ['Fruit','Colour'];
    for table in tables:
      rows = db_conn.execute("SELECT ID FROM %s" % table)
      for row in rows:
        if IDs.has_key(row['ID']):
          print "Duplicate ID %s is present in both %s and %s" % (row['ID'], table, IDs[row['ID']])
        else:
          IDs[row['ID']] = table
    

    It doesn’t give quite te same output (especially when a key is present in 3 tables it won’t show all the permutations), but it will quickly show you where your problems are.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.