import pickle from django db import connection results pickle load ope

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import pickle
from django.db import connection
results = pickle.load(open('result_ranks.dump'))
# get unique query_texts
query_texts = list(set([r[1] for r in results]))
db_queries = []
# new queries
db_queries.append(r"INSERT INTO `rr_query` (id) VALUES " + \
",".join(["(%d)"%i for i in range(1,len(query_texts)+1)]))
# rr_criterias for queries
db_queries.append(r"INSERT INTO `rr_criteria` (type_id, value, query_id) "\
" VALUES " + ",".join( ["(1, '%s', %d)" % (q, query_texts.index(q)+1)\
for q in query_texts] ))
# ratings
db_queries.append(r"INSERT INTO `result_rank` (user_id, content_type_id, object_id, "\
"rating_id, query_id, date) VALUES " + \
", ".join( ["(%d, 10, '%s', %d, %d, '%s')" % (r[0], r[3],
r[4], query_texts.index(r[1])+1, r[5]) for r in results] ))
cursor = connection.cursor()
for q in db_queries:
print q
if raw_input("Continue? [N/y]") == 'y':
cursor.execute(q)