· 9 years ago · Oct 10, 2016, 10:22 PM
1#! /usr/bin/env python
2import psycopg2
3try:
4 from StringIO import StringIO
5except ImportError:
6 from io import StringIO
7
8conn = psycopg2.connect(database = 'postgres', user = 'postgres', password='')
9cur = conn.cursor()
10rounds = range(14, 16)
11
12
13class InsertPlanetRank:
14 basename = 'planet_ranks_round_'
15 for i in rounds:
16 try:
17 cur.execute(
18 """CREATE TABLE IF NOT EXISTS round_%d_planet_rank (
19 planet_rank serial PRIMARY KEY, ruler_name text, planet_name text,
20 nickname text, alliance_name text,
21 planet_score integer, planet_value integer,
22 planet_xp integer, planet_size integer,
23 planet_race text, planet_coords text
24 );"""
25 % i)
26 except IOError:
27 print('ERROR: something went wrong creating the tables.')
28 try:
29 io = open(str('dumps/planet_ranks_round_%d.csv' % i), 'r')
30 cur.copy_from(io, str('round_%d_planet_rank' % i), sep=',')
31 except IOError:
32 print('ERROR: something went wrong inserting the data.')
33 conn.commit()
34 cur.close()
35 conn.close()