ITPub博客

首页 > 应用开发 > > 求高手指教 !

求高手指教 !

作者:doudourabbit 时间:2019-07-29 13:25:59 0 删除 编辑

用Python 从Mysql中提取数据并将查询结果写进excel,但返回值不是SQL查询值 而是‘0’求高手指教

第一步:使用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’,请问高手们,这个是为什么?要怎么破呢?

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4809/viewspace-3122418/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-25

  • 博文量
    193
  • 访问量
    1437