Python+mysql编写一个简单的代码量记录器

  • 已然大四,想当初大一时在某处听大神教诲:
    大学四年写足十万行代码,码路遂成;

  • 前几天想想是该把欠下的码量补上了,于是花了一个晚上写了个代码量记录器,其实也很简单:用Python写个简陋的展示界面和与数据库交互的按钮,用MySQL保存插入的数据;

  • 大概就是这样子:


    Python+mysql编写一个简单的代码量记录器_第1张图片
    1.png
Python+mysql编写一个简单的代码量记录器_第2张图片
2.png
  • 码量一行填入要提交的代码量,语言默认是C++,备注自己标注,点击commit按钮则提交, refresh按钮从数据库读取数据并刷新页面;
  • 建表语句是:
create table codeRecord
(
code_id int auto_increment,
code_date date not null default now(),
code_line int not null default 0,
code_type varchar(10) not null default 'C++',
code_comment varchar(20) default " ",
primary key(code_id)
)engine=innodb default charset=utf8;
  • Python版本是2.7 ,UI是用Python自带的tkinter库写的,虽然有点难看,但自己用也还凑合,与MySQL进行交互的库是用mysql-connector-python,这个是第三方的,需要自己下载并安装到环境里,Google一番很容易;

  • 用数据库表记录下插入的记录,方便以后做各种统计和分析

  • 下面直接把代码贴上来吧:

#!/usr/bin/python
# -*- coding:UTF-8 -*-
from Tkinter import *
import tkFont
import mysql.connector as mariadb

codeTool = Tk()

config = {
'host':'127.0.0.1',  # 如果是跑在本地
'user':'xxxx',  # 数据库用户名
'password':'xxxxx',   # 数据库密码
'port':3306,
'database':'xxxxx',    # 数据库名
'charset':'utf8'
}

feedbackText = StringVar()
feedbackText.set(' ')
codeLineText = StringVar()
codeTypeText = StringVar()
codeCommentText = StringVar()
codeTodayText = StringVar()
codeTotalText = StringVar()

def func_GUI(codeTool):
    codeTool.title("十万行代码")
    
    codeTool.minsize(400, 300)
    # 字体
    labFont = tkFont.Font(family='Fixdsys', size=25, weight=tkFont.BOLD)

    codeLineLab = Label(codeTool, text="码量:  ", font=labFont)
    codeLineEnt = Entry(codeTool, textvariable=codeLineText)
    codeTypeLab = Label(codeTool, text="语言(默认:C++):  ")
    codeTypeEnt = Entry(codeTool, textvariable=codeTypeText)
    codeCommentLab = Label(codeTool, text="备注(默认为空):  ")
    codeCommentEnt = Entry(codeTool, textvariable=codeCommentText)
    codeCommitBtn = Button(codeTool, text='Commit', command=func_codeCommit, width=6)
    codeRefreshBtn = Button(codeTool, text='Refresh', command=func_codeRefresh, width=6)

    showFont = tkFont.Font(size=40, weight=tkFont.BOLD)
    codeTodayLab = Label(codeTool, text="今日代码量:    ", font=labFont)
    codeTodayNum = Label(codeTool, text=" ", textvariable=codeTodayText, font=showFont)
    codeTotalLab = Label(codeTool, text="总代码量:      ", font=labFont)
    codeTotalNum = Label(codeTool, text=" ", textvariable=codeTotalText, font=showFont)

    codeFeedbackLab = Label(codeTool, text='连接错误', textvariable=feedbackText)

    codeLineLab.grid(row=0, column=0)
    codeLineEnt.grid(row=0, column=1)
    codeTypeLab.grid(row=1, column=0)
    codeTypeEnt.grid(row=1, column=1)
    codeCommentLab.grid(row=2, column=0)
    codeCommentEnt.grid(row=2, column=1)

    codeCommitBtn.grid(row=3, column=0)
    codeRefreshBtn.grid(row=3, column=1)

    codeTodayLab.grid(row=4, column=0)
    codeTodayNum.grid(row=4, column=1)

    codeTotalLab.grid(row=5, column=0)
    codeTotalNum.grid(row=5, column=1)

    codeFeedbackLab.place(x=10, y=250)

    return codeTool

def func_codeRefresh():
    try:
        conn = mariadb.connect(**config)
    except Exception , e:
        print e
        feedbackText.set('连接失败')
        conn.close()
        return
    cursor = conn.cursor()

    sqlToday = "select sum(code_line) as code_line from codeRecord where code_date = curdate();"
    sqlTotal = "select sum(code_line) as code_line from codeRecord;"

    try:
        cursor.execute(sqlToday)
        for cl in cursor:
            tVal = cl[0] if cl[0] else "0"
            codeTodayText.set(tVal)
        cursor.execute(sqlTotal)
        for cl in cursor:
            tVal = cl[0] if cl[0] else "0"
            codeTotalText.set(tVal)
    except Exception , e:
        feedbackText.set('查询错误')
        cursor.close()
        conn.close()

    cursor.close()
    conn.close()


def func_codeCommit():
    try:
        conn = mariadb.connect(**config)
    except Exception, e:
        print e
        feedbackText.set('连接失败')
        conn.close()
        return
    cursor = conn.cursor()
    codeNum = codeLineText.get()
    codeType = codeTypeText.get()
    codeComment = codeCommentText.get()
    codeType = "C++" if codeType=="" else codeType
    codeComment = "forFun" if codeComment=="" else codeComment

    sql = "insert into codeRecord(code_line, code_type, code_comment) values(%s, %s, %s);"
    param = (codeNum, codeType, codeComment)
    try:
        cursor.execute(sql, param)
        conn.commit()
    except Exception, e:
        print e
        feedbackText.set('插入失败')
        cursor.close()
        conn.close()
        return
    feedbackText.set('插入成功:'+codeNum)
    cursor.close()
    conn.close()
    #更新下数据
    func_codeRefresh()


if __name__ ==  "__main__":
    func_GUI(codeTool)
    codeTool.mainloop()

  • 代码很简单,一看便知;

你可能感兴趣的:(Python+mysql编写一个简单的代码量记录器)