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_section(id, code, name, owner, the_geom): with psycopg2.connect(G) as connection: with connection.cursor() as cursor: sql = "insert into base_section (id, code, name, owner, rtu, box, the_geom) values ('%s', '%s', '%s', '%s', false, false, st_geometryfromtext('%s', 4326))" % ( id, code, name, owner, the_geom) if the_geom is None: sql = "insert into base_section (id, code, name, owner, rtu, box) values ('%s', '%s', '%s', '%s', false, false)" % ( id, code, name, owner) print sql cursor.execute(sql) connection.commit() if __name__ == '__main__': with psycopg2.connect(GT) as connection: with connection.cursor() as cursor: cursor.execute( "select id, code, name, owner, st_astext(the_geom) from base_section where code like 'SZ%' and code != 'SZD' ") results = cursor.fetchall() for r in results: id = r[0] code = r[1] name = r[2] owner = r[3] the_geom = r[4] print code, name, owner, the_geom insert_section(id, code, name, owner, the_geom)