Created
February 1, 2015 19:57
-
-
Save meeuw/221cc7e7c3f28abe09b8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
import json | |
table = 'table' | |
with open('output.csvdiff') as f: | |
diff = json.loads(f.read()) | |
for added in diff["added"]: | |
print "INSERT INTO %s (%s) VALUES ('%s');" % (table, ", ".join(added.keys()), "', '".join(added.values())) | |
for changed in diff["changed"]: | |
fields = [] | |
for field, value in changed["fields"].iteritems(): | |
fields.append("`%s` = '%s'" % (field, value["to"])) | |
keys = [] | |
for i in range(len(changed["key"])): | |
keys.append("`%s` = '%s'" % (diff["_index"][i], changed["key"][i])) | |
print "UPDATE %s SET %s WHERE %s;" % (table, ", ".join(fields), ", ".join(keys)) | |
for removed in diff["removed"]: | |
keys = [] | |
for i in range(len(diff["_index"])): | |
keys.append("`%s` = '%s'" % (diff["_index"][i], removed[diff["_index"][i]])) | |
print "DELETE FROM %s WHERE %s;" % (table, ", ".join(keys)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment