在项目使用中有时候会需要给用户拉一下数据,那么python脚本就很好的解决了这个问题。但是python2和python3在使用库中有些不一样。
代码如下:
# -*- coding: utf-8 -*-
import math
import sys
import pymysql
# pymysql.install_as_MySQLdb()
import xlwt
# import MySQLdb
print (sys.argv[1])
print (sys.argv[2])
number1 = sys.argv[1]
number2 = sys.argv[2]
sqlString = 'select t.id, t.schoolid, t.czr, t.xqid, t.zy, q.title from questionnaire_time_grant t, tquestionnaire q where sfsc = 0 and schoolid= ** and status= 1 AND q.isdelete = 0 AND q.id = t.wjid limit ' + number1 +' , '+ number2
print (sqlString)
conn = pymysql.connect(
host='****',
port=***,
user=***,
passwd=***,
db=***,
charset='utf8'
)
print (conn.cursor())
cursor = conn.cursor()
count = cursor.execute(sqlString)
print (count)
#重置游标位置
cursor.scroll(0,mode='absolute')
#搜取所有结果
results = cursor.fetchall()
# fields = cursor.description
fields = ['id','schoolid','czr','xqid','zy','title']
print (fields)
#print (results)
wbk = xlwt.Workbook(encoding='utf-8')
sheet = wbk.add_sheet('test1',cell_overwrite_ok=True)
style = xlwt.XFStyle()#初始化样式
font = xlwt.Font()#创建字体
font.name = u'微软雅黑' #字体类型
font.colour_index = 6 #字体颜色
print (len(fields))
# for ifs in range(0,len(fields)):
# sheet.write(0,ifs,fields[ifs][0])
for ifs in range(0,len(fields)):
sheet.write(0,ifs,fields[ifs])
ics=1
jcs=0
for ics in range(1,len(results)+1):
for jcs in range(0,len(fields)):
sheet.write(ics,jcs,results[ics-1][jcs])
cursor.close()
conn.close()
wbk.save('test4.xls')
刚学python,有什么问题欢迎交流。