经常要去线上做一些数据订正,使用java还是太重了,sql脚本又太复杂,使用python可以轻松搞定。
环境
import MySQLdb conn = MySQLdb.connect(host='localhost',user='root',passwd='') cursor = conn.cursor() cursor.execute("create database python") cursor.execute('use python'); cursor.execute("create table test(id int, content varchar(100))") #插入一条100条数据 for i in range(1,100): cursor.execute("insert into test values(%s,%s)",[i,'haha']); #获取数据 cursor.execute('select * from test') results = cursor.fetchall() for r in results print r conn.close()