修改MySQL数据库里面的内容相当繁琐, 网站搜索的方式都不能满足我的需求,今天我用python简单写了一个修改logo的URL地址的,可以很方便的修改名称
代码如下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-11-13 16:56
# @Author : Xinru
import pymysql
db = pymysql.connect(host='127.0.0.1', port=3306, db='movies', user='root', passwd='qwer1234', charset='utf8')
def get_mysql(sql):
try:
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
cursor.execute(sql)
data = cursor.fetchall()
return data
except:
return '发生错误'
# finally:
# cursor.close()
# db.close()
for id in range(18960):
sql = "select logo from bt2 where id = %d;" % (id)
try:
data = get_mysql(sql)
# print(data)
data1 = data[0][0]
data2 = data1.split('/')[-1][:-6]
# print(id, data1, data2)
sql1 = "update bt2 set logo = replace(logo,'%s','%s') where id=%d;"
conn = db.cursor()
conn.execute(sql1 % (data1, data2, id))
db.commit()
conn.close()
except:
print('修改失败', 'id:', id)
网友评论