第一步:使用Python 从Mysql中提取数据import pymysql
conn = pymysql.connect(host='localhost',user='root',passwd='QAZwsx12345678',db='test',port=3306,charset='utf8')
cur = conn.cursor()
cur.execute("use test")
cur.execute("SELECT Stu_Id FROM student WHERE student.`Name`='王老大'")
a = cur.fetchall()
b = a.__str__()
print(b)
cur.close()
conn.close返回如图:第二部:使用GUI端,将Mysql的查询结果一键写进excel
import tkinter
import os
import openpyxl
from tkinter import *
class control :
def __init__(self, master):
fm1 = Frame(master)
Button(fm1, text='Top',command = self.exc).pack(side=TOP, anchor=W, fill=X, expand=NO,padx = 10, pady = 10,ipadx=10,ipady = 4)
fm1.pack(side=LEFT, fill=BOTH, expand=YES)
def exc(self):
wb = openpyxl.load_workbook('C:/Users/lenovo/Desktop/学生.xlsx')
sheet = wb.get_sheet_by_name('学生1')
sheet['A2'] = os.system('C:/Users/lenovo/PycharmProjects/Test.9.16/tests/New_created.py')
print(sheet['A2'].value)
wb.save('学生_copy.xlsx')
root = Tk()
root.title("Control")
display = control(root)
root.mainloop()点击GUI按键后得到 返回值 如图:结果:但打开excel 后 发现添加的内容并非SQL的返回值,而是‘0’,请问高手们,这个是为什么?要怎么破呢?
添加回答
举报
0/150
提交
取消