import uuid import psycopg2 GT = 'host=210.77.68.250 port=5432 dbname=g214 user=g214 password=g214G214' G = 'host=210.77.68.250 port=5432 dbname=g214db user=g214 password=g214G214' def insert_probe(code, name, type, depth, the_geom, section_id): with psycopg2.connect(G) as connection: with connection.cursor() as cursor: id = uuid.uuid4() sql = "insert into base_drilling (id, code, name, type, depth, the_geom, section_id) values ('%s', '%s', '%s', '%s', '%s', st_geometryfromtext('%s', 4326), '%s')" % ( id, code, name, type, depth, the_geom, section_id) if the_geom is None: sql = "insert into base_drilling (id, code, name, type, section_id) values ('%s', '%s', '%s', '%s', '%s')" % ( id, code, name, type, section_id) print sql cursor.execute(sql) connection.commit() if __name__ == '__main__': with psycopg2.connect(GT) as connection: with connection.cursor() as cursor: cursor.execute( "select code, name, type, depth, st_astext(the_geom), section_id from base_drilling where section_id in (select id from base_section where code like 'SZ%' and code != 'SZD')") results = cursor.fetchall() for r in results: code = r[0] i = code.find('.') if i != -1: code = int(float(code)) name = r[1] type = r[2] depth = r[3] the_geom = r[4] section_id = r[5] print code, name, type, depth, the_geom insert_probe(code, name, type, depth, the_geom, section_id)