Last active
August 29, 2015 14:03
-
-
Save jiaaro/6256f414da95be5e7ef6 to your computer and use it in GitHub Desktop.
create hstore col
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import connection | |
def hs_str(s): | |
s = unicode(s) | |
return s.replace('"', r'\"') | |
def dict_to_hstore(d): | |
hs_data = [u'"{0}" => "{1}"'.format(hs_str(k), hs_str(v)) for (k, v) in data.items()] | |
hs_data = u",".join(hs_data) | |
return "u'{0}'::hstore".format(hs_data) | |
cursor = connection.cursor() | |
i = 0 | |
while True: | |
pids = list(Participant.objects.values_list('id', flat=True)[i:i+1000]) | |
if not pids: | |
break | |
query = u"" | |
for pid, data in Participant.objects.get_answer_data(pids): | |
if not data: | |
continue | |
query += u"UPDATE stitcher_participant SET hs_data = %s;" % dict_to_hstore(data) | |
cursor.execute(query) | |
i += 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment