Skip to content

Instantly share code, notes, and snippets.

@websofter
Created July 1, 2021 03:18
Show Gist options
  • Select an option

  • Save websofter/7aa87ba822e6f4aaf45faac3abf6377c to your computer and use it in GitHub Desktop.

Select an option

Save websofter/7aa87ba822e6f4aaf45faac3abf6377c to your computer and use it in GitHub Desktop.
Insert bulk data into table in Django/PostgreSQL
def insert_cities(cities):
dbname, tablename = 'MyDB', 'city_coordinates'
with connections[dbname].cursor() as cursor:
for city in cities:
cursor.execute(f"""
INSERT INTO {tablename} (record_id, X, Y, record_id_2, city_id, city_name_heb, secondary_id, city_type_id, city_type_name, cuty_name_eng)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
(city['record_id'], city['X'], city['Y'], city['record_id_2'], city['city_id'], city['city_name_heb'], city['secondary_id'], city['city_type_id'], city['city_type_name'], city['city_name_eng']))
connections[dbname].commit()
cursor.close()
connections[dbname].close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment