Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-.
- import os
- import MySQLdb
- import json
- sql_filename = '/home/lotsof/www/migrate.sql.txt'
- db = MySQLdb.connect(
- host=mysql_host,
- user=mysql_user,
- passwd=mysql_password,
- db=mysql_database,
- charset='utf8',
- use_unicode=True)
- cur = db.cursor()
- cur.execute('SET NAMES UTF8')
- 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')
- with open(sql_filename, 'wb') as fd:
- for row in cur.fetchall():
- if row[0] is None:
- phone = 'NULL'
- else:
- phone = '%s' % row[0]
- if row[1] is None:
- email = 'NULL'
- else:
- email = '%s' % row[1]
- name = ''
- if not row[2] is None:
- name = row[2]
- name += ' '
- if not row[3] is None:
- name += row[3]
- if name == ' ':
- name = 'NULL'
- comment = {}
- if row[4] is None:
- comment['sity'] = 'NULL'
- else:
- # print type(row[4])
- #print row[4].encode('utf-8')
- comment['sity'] = row[4]
- #print comment['sity']
- #print type(row[4])
- comment = json.dumps(comment)
- # print comment
- #comment = "'%s'" % comment
- geoid = '42'
- sqlsting = 'INSERT INTO crmclientmash ("phone", "email", "name", "geo_id", "shortDescription") VALUES({phone}, {email}, {name}, {geoid}, {comment});\n'.format(
- phone=phone, email=email, name=name, geoid=geoid, comment=comment)
- fd.write(sqlsting.encode('utf8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement