import pymysql,xlwt

#1、连接mysql
#2、执行sql语句
#3、获取到sql执行结果
#4、写入excel
def conn_mysql(sql):
    conn = pymysql.connect(host='211.149.218.16',user='jxz',password='123456',db='jxz',charset='utf8')
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cur.execute(sql)
    res = cur.fetchone()
    print(res)
    conn.commit()
    cur.close()
    conn.close()
    return res

def write_excel(file_name,content):
    book = xlwt.Workbook()
    sheet = book.add_sheet('sheet1')
    line_no = 0#控制行数
    for line in content:
        row = 0#控制列数
        for j in line:
            sheet.write(line_no,row,j)
            row+=1
        line_no+=1
    book.save(file_name)
res = conn_mysql("insert into jxz_stu  (name,cl,c2,c3) values ('牛寒阳','交','交','交');")
write_excel('lanxia.xls',res)

 

相关文章:

  • 2022-12-23
  • 2021-12-09
  • 2021-11-23
  • 2021-08-15
  • 2021-09-29
  • 2021-06-15
  • 2022-12-23
  • 2021-04-10
猜你喜欢
  • 2022-02-04
  • 2021-09-02
  • 2021-12-21
  • 2021-05-06
  • 2021-09-28
相关资源
相似解决方案