python - 怎么写一个脚本可以清除数据库里的某行测试数据
阿神
阿神 2017-05-18 10:50:07
0
1
973

就是要写个脚本 数据库有测试用的手机号 注册完数据库里会有一大堆相关的数据 然后脚本就需要能清除这些数据 或者将它们设置为null 这个是类似的一个脚本 应该怎么修改

from future import print_function
import itertools
import pymysql.cursors
import csv

from utils import is_telephone, is_email

if name == '__main__':
sg_connection = pymysql.connect(
host='localhost',
user='root',
password='root',
db='sellergrowth',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor
)

uc_connection = pymysql.connect(
host='localhost',
user='root',
password='root',
db='sguc',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor
)

def _filter_valid(entity):
if not entity['username']:
return False
if entity['telephone'] and not is_telephone(entity['telephone']):
return False
if entity['email'] and not is_email(entity['email']):
return False
if not (
(entity['telephone'] and entity['telephone_verified']) or (entity['email'] and entity['email_verified'])):
return False
return True

def _map_to_correct(entity):
def _set_blank_to_none(name):
if not entity[name]:
entity[name] = None

_set_blank_to_none('telephone')
_set_blank_to_none('email')
if entity['telephone'] and not entity['telephone_verified']:
entity['telephone'] = None
if entity['email'] and not entity['email_verified']:
entity['email'] = None
_set_blank_to_none('password')
_set_blank_to_none('wechat_unionid')
_set_blank_to_none('wechat_app_openid')
_set_blank_to_none('wechat_public_openid')
_set_blank_to_none('wechat_site_openid')
_set_blank_to_none('qq_openid')
_set_blank_to_none('last_login')
_set_blank_to_none('sign_url')
_set_blank_to_none('user_uuid')
if not entity['signup_service_id']:
entity['signup_service_id'] = 11300
if entity['signup_method'] == 1:
entity['signup_method'] = 'MOBILE'
elif entity['signup_method'] == 2:
entity['signup_method'] = 'TABLET'
elif entity['signup_method'] == 3:
entity['signup_method'] = 'WE_CHAT_PUBLIC'
elif entity['signup_method'] == 4:
entity['signup_method'] = 'WE_CHAT_SITE'
elif entity['signup_method'] == 12:
entity['signup_method'] = 'WE_CHAT_APP'
elif entity['signup_method'] == 6:
entity['signup_method'] = 'QQ_WEB'
elif entity['signup_method'] == 7:
entity['signup_method'] = 'QQ_WEBAPP'
else:
entity['signup_method'] = None
entity['is_active'] = 1 if entity['is_active'] else 0
return entity

def _dict_to_tuple(entity):
return tuple((
entity['电话'], 实体['电子邮件'], 实体['用户名'], 实体['密码'], 实体['wechat_unionid'],
实体['wechat_app_openid'],
entity['wechat_public_openid'], 实体['wechat_site_openid'], 实体['qq_openid'],
int(entity['signup_service_id']), 实体['date_joined'], 实体['last_login '], 实体['is_active'],
实体['signup_method'], 实体['sign_url'], 实体['user_uuid']
))

尝试:
以 sg_connection.cursor() 作为光标,uc_connection.cursor() 作为 uc_cursor:
sql = 'SELECT is_active, p.电话, 电子邮件, 用户名, 密码, p. wechat_unionid,p.wechat_app_openid,p.wechat_public_openid,p.wechat_site_openid,p.qq_openid,p.signup_service_id,p.telephone_verified, p.email_verified, date_joined, last_login、p.signup_method、e.sign_url、p2.user_uuid 来自 account_profile p LEFT JOIN event_baseevent e ON e.id=p.signup_event_id LEFT JOIN account_profile p2 ON p2.user_id=p.inviter_id, auth_user
u WHERE p.user_id=u.id'
cursor.execute(sql)
cursor,cursor_for_record = itertools.tee(cursor)
cursor = itertools.ifilter(_filter_valid,cursor)
cursor = itertools.imap(_map_to_ Correct,cursor)

cursor = itertools.imap(_dict_to_tuple, 游标)

sql = 'INSERT into cas_service
(service_id, service_group) VALUES (%s, %s)'
service_list = ((11300, 'SG'), (12300, 'SGASK'), (13300, 'YQSXY') , (14300, 'CHUANGLAN'))
uc_cursor.executemany(sql, service_list)

uc_connection.commit()

insert_sql = '插入cas_user
(电话,电子邮件,用户名1,密码1,wechat_unionid,wechat_app_openid,wechat_public_openid,wechat_site_openid,qq_openid,sign_up_service_id,date_joined,last_login,is_active,sign_up_device,sg_event_url, _uid) 值 (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
uc_cursor.executemany(insert_sql,列表(光标))

uc_connection.commit()


with open('not_migration_report.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
init = False
cursor_for_record = itertools.ifilterfalse(_filter_valid,cursor_for_record)
for i incursor_for_record:
如果不是 init:
init = True
print(i.keys())
spamwriter.writerow(
[a.encode(“utf8”)如果类型(a) == unicode else a for a in i.values()])
最后:
sg_connection.close()

uc_connection.close()🎜
阿神
阿神

闭关修行中......

全部回复(1)
迷茫

如果对表有操作权限的话 只需要 增加一个标识状态的flag即可 如 flag = test

DELETE * FROM table_name WHERE flag = test
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板