Advertisement
METAJIJI

Untitled

Mar 13th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-.
  3.  
  4. import os
  5. import MySQLdb
  6. import json
  7.  
  8. sql_filename = '/home/lotsof/www/migrate.sql.txt'
  9.  
  10. db = MySQLdb.connect(
  11. host=mysql_host,
  12. user=mysql_user,
  13. passwd=mysql_password,
  14. db=mysql_database,
  15. charset='utf8',
  16. use_unicode=True)
  17.  
  18. cur = db.cursor()
  19. cur.execute('SET NAMES UTF8')
  20.  
  21. cur.execute('SELECT user.phone, user.email, user.first_name, user.last_name, user.city, user.image_id from user, shop where user.id=shop.user_id')
  22. with open(sql_filename, 'wb') as fd:
  23. for row in cur.fetchall():
  24. if row[0] is None:
  25. phone = 'NULL'
  26. else:
  27. phone = '%s' % row[0]
  28. if row[1] is None:
  29. email = 'NULL'
  30. else:
  31. email = '%s' % row[1]
  32. name = ''
  33. if not row[2] is None:
  34. name = row[2]
  35. name += ' '
  36. if not row[3] is None:
  37. name += row[3]
  38. if name == ' ':
  39. name = 'NULL'
  40. comment = {}
  41. if row[4] is None:
  42. comment['sity'] = 'NULL'
  43. else:
  44. # print type(row[4])
  45. #print row[4].encode('utf-8')
  46. comment['sity'] = row[4]
  47. #print comment['sity']
  48. #print type(row[4])
  49.  
  50. comment = json.dumps(comment)
  51. # print comment
  52. #comment = "'%s'" % comment
  53. geoid = '42'
  54. sqlsting = 'INSERT INTO crmclientmash ("phone", "email", "name", "geo_id", "shortDescription") VALUES({phone}, {email}, {name}, {geoid}, {comment});\n'.format(
  55. phone=phone, email=email, name=name, geoid=geoid, comment=comment)
  56. fd.write(sqlsting.encode('utf8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement