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

  • Home
  • SEARCH
  • 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 6999643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:34:06+00:00 2026-05-27T20:34:06+00:00

I use the following python3 code to add and update entries in an sqlite3

  • 0

I use the following python3 code to add and update entries in an sqlite3 database:

def increment_person_counts(count_per_person):
   with sqlite3.connect(r'./people_database') as connection:
      cursor = connection.cursor()
      for person in count_per_person:
         if cursor.execute('select * from personCounts where person = ?', [person]).fetchone()==None:
            cursor.execute('insert into personCounts(person, count) values (?, ?)', [person, count_per_person[person]])
         else:
            cursor.execute('update personCounts SET count=count + ? WHERE person=?', [count_per_person[person], person])
      connection.commit()

count_per_person contains 4 million entries, and I seem to be able to add/update around 100 entries per second which means it will take half a day to add these values. Is there a better/faster approach to doing this that I should consider?

Thanks for your help,

Barry

  • 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-27T20:34:07+00:00Added an answer on May 27, 2026 at 8:34 pm

    You can read your whole 'select * from personCounts' into a python set() at the beginning and then check just against this set.

    def increment_person_counts(count_per_person):
       with sqlite3.connect(r'./people_database') as connection:
          cursor = connection.cursor()
          cursor.execute('select person from personCounts')
          known_persons = set(row[0] for row in cursor.fetchall())
          for person, count in count_per_person.iteritems():
             if person in known_persons:
                cursor.execute('insert into personCounts(person, count) values (?, ?)', [person, count])
             else:
                cursor.execute('update personCounts SET count=count + ? WHERE person=?', [count, person])
          connection.commit()
    

    UPDATE: after my comment, here is the update with executemany:

    def increment_person_counts(count_per_person):
        with sqlite3.connect(r'./people_database') as connection:
            cursor = connection.cursor()
            cursor.execute('select person from personCounts')
            known_persons = set(row[0] for row in cursor.fetchall())
            cursor.executemany('insert into personCounts(person, count) values (?, ?)', ((person, count) for count_per_person.iteritems() if person in known_persons))
            for person, count in count_per_person.iteritems():
                if person not in known_persons:
                    cursor.execute('update personCounts SET count=count + ? WHERE person=?', [count, person])
            connection.commit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following code to temporarily modify environment variables. @contextmanager def _setenv(**mapping): ``with``
I have the following IronPython code. class Hello: def __init__(self): pass def add(self, x,
I use the following code to create countdowns in Javascript. n is the number
I use the following code to compile a cpp file to object file. g++
I have the following python code: try: pr.update() except ConfigurationException as e: returnString=e.line+' '+e.errormsg
As of now I use the following python code: file = open(filePath, r) lines=file.readlines()
I'm trying to run the following code in python3, but it has been written
I use the following python code to download web pages from servers with gzip
I use the following method to break the double loop in Python. for word1
for example i use following command to find a record SELECT `users`.`mail` FROM `users`

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.