记录小菜鸟的学习历程,代码可参考
import pymysql
db =pymysql.connect(host='localhost',port=3306,user='root',password='1234',
database='sunmin',charset='utf8')
cursor = db.cursor()
sql = "select count(id) from customer where id between %s and %s"
xlsx_list = []
for i in range(1,30000,1000):
section = list((i,i+999))
cursor.execute(sql,section)
aa = cursor.fetchone()
x = str(section)+"区间对应数量为"+str(aa[0])
xlsx_list.append(x)
result = open('D:pythonspiderexample.xls', 'w', encoding='gbk')
# 参数'w'表示往指定表格读入数据,会先将表格中原本的内容清空
# 若把参数’w'修改为‘a+',即可实现在原本内容的基础上,增加新写入的内容
for i in range(0, len(xlsx_list)):
result.write(str(xlsx_list[i]))
#result.write('t') # 't'表示每写入一个元素后,会移动到同行的下一个单元格
result.write("n") # 换行操作
result.close()
db.close()


