Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def insert_into_table(data):
- # print json.dumps(data, indent=4)
- conn = psycopg2.connect(database='testdb', user='postgres', password='password', host='localhost')
- cursor = conn.cursor()
- for i in data:
- # print json.dumps(i, indent=4)
- iso_code = i["iso_code"]
- if i["l_postcode"] is None:
- l_postcode = 'NULL'
- else:
- l_postcode = i["l_postcode"]
- if i["r_postcode"] is None:
- r_postcode = 'NULL'
- else:
- r_postcode = i["r_postcode"]
- link_id = i["link_id"]
- geom = i["geometry"]
- # sql = 'INSERT into data_load VALUES ('{}', {}, {},{} , st_geomfromgeojson('{}'))'.format(iso_code, l_postcode, r_postcode, link_id, json.dumps(geom))
- sql = """INSERT into data_load (iso_code, l_postcode, r_postcode, link_id, geom) VALUES ('{}',{},{},{},st_geomfromgeojson('{}'));""".format(iso_code, l_postcode, r_postcode, link_id, json.dumps(geom))
- print sql
- cursor.execute(sql, )
- print sql
- cursor.close()
- conn.commit()
- conn.close()
- [
- {
- "geometry": {
- "type": "LineString",
- "coordinates": [
- [
- -91.98979,
- 15.644559999999998
- ],
- [
- -91.98971,
- 15.645249999999999
- ]
- ]
- },
- "iso_code": "MEX",
- "l_postcode": null,
- "r_postcode": null,
- "link_id": 1186786776
- },
- {
- "geometry": {
- "type": "LineString",
- "coordinates": [
- [
- -106.77742,
- 28.390159999999998
- ],
- [
- -106.77806,
- 28.39076
- ]
- ]
- },
- "iso_code": "MEX",
- "l_postcode": null,
- "r_postcode": null,
- "link_id": 818231403
- },
- {
- "geometry": {
- "type": "LineString",
- "coordinates": [
- [
- -98.89940999999999,
- 18.90605
- ],
- [
- -98.89926,
- 18.906689999999998
- ]
- ]
- },
- "iso_code": "MEX",
- "l_postcode": null,
- "r_postcode": null,
- "link_id": 1130886811
- }
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement