Created
July 1, 2021 03:18
-
-
Save websofter/7aa87ba822e6f4aaf45faac3abf6377c to your computer and use it in GitHub Desktop.
Insert bulk data into table in Django/PostgreSQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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