更新代码

master
bettleChen 11 months ago
parent b3b699125d
commit ddc3ec0db1

@ -13,5 +13,4 @@ class BaseWindow(Frame):
self.master = master self.master = master
self.font_style = tkFont.Font(family="Lucida Grande", size=20) self.font_style = tkFont.Font(family="Lucida Grande", size=20)
self.button_font = tkFont.Font(family="Lucida Grande", size=10) self.button_font = tkFont.Font(family="Lucida Grande", size=10)
self.conn = pymysql.connect(user="root", password="cyh0110", database="SCT", host="127.0.0.1", port=3306) self.conn = pymysql.connect(user="root", password="123123", database="SCT", host="127.0.0.1", port=3306)

@ -1,20 +1,16 @@
# 数据库思维与关系数据库系统仿真 # 项目编程基本原则
## 关系数据库系统项目介绍
### 关系数据库系统项目开发的总体思路 ## 文档及代码编辑的注意事项
<p style="text-indent:2em;">本章后续内容将以关系数据库的定义与操纵为目标拟开发一个可交互式练习SQL语句构造与执行的系统使读者更好地体验关系数据库系统管理大规模数据的基本思维。掌握SQL语言以及基于SQL语言的数据库应用程序的开发和运行。</p> ### 进度汇报
**查文档和编程任务是否符合。**
【目标系统说明】。可交互式练习SQL语句构造与执行的系统1建立一个示例数据库SCT 内中包含3个数据表Student, SC和Course注意本书是以SCT为例读者做实验时可选择建立其他的数据库和数据表2利用Insert、Delete、Update语句对示例数据库的各个表追加内容3利用Select语句检索示例数据库并显示检索结果4编制应用程序使用户可以针对固定的表输入检索条件系统自动构造SQL语句并执行该SQL语句完成检索并输出检索结果。5编制应用程序使用户可以对数据库中的任何表进行检索条件输入并构造SQL语句、执行SQL语句显示检索结果。 首先自查一遍文档,每个编程任务是否符合文档上编程任务的要求---请在进度表中标示【自查:符合 或 不符合】是按编程任务不是按X1,X2...
【项目开发总体思路】。关系数据库系统项目开发的总体思路如图示意。分为5个层面的开发内容难度由低到高的被区分为
- X1数据的变化与基本控制【数据结构】
- X2屏幕元素及其显示输出【用户界面】
- X3问题求解相关算法设计与函数实现【算法】
- X4屏幕元素事件识别与控制【人机交互】
- X5问题求解相关的算法优化与系统功能拓展【完整系统】
最终开发的程序可以由用户交互式操作(不用编写程序)来模拟体验数据库系统管理大规模结构化数据的过程。
**查命名问题,函数框架,函数名称,和变量名规范**
(2)按我们前两天讨论的,主函数框架、函数关系调用图、函数名称与变量名称规范化等方面回复【已优化完成,未优化完成】
### 主函数框架示例: ### 主函数框架示例:

@ -22,8 +22,9 @@ class LoadElement(Frame):
self.master = master self.master = master
self.table_name = table_name self.table_name = table_name
self.data = [] self.data = []
self.conn = pymysql.connect(user="root", password="cyh0110", database="SCT", host="127.0.0.1", port=3306) self.conn = pymysql.connect(user="root", password="123123", database="stu_sys", host="127.0.0.1", port=3306)
self.data_frame = pd.read_sql(f"select * from {self.table_name} limit 0", self.conn) self.data_frame = pd.read_sql(f"select * from {self.table_name} limit 0", self.conn)
#左侧三框框数据
self.font_style = tkFont.Font(family="Lucida Grande", size=20) self.font_style = tkFont.Font(family="Lucida Grande", size=20)
self.button_font = tkFont.Font(family="Lucida Grande", size=10) self.button_font = tkFont.Font(family="Lucida Grande", size=10)
self.create_widget() self.create_widget()
@ -46,11 +47,11 @@ class LoadElement(Frame):
combo["values"] = tables combo["values"] = tables
combo.bind('<<ComboboxSelected>>', self.select_value) combo.bind('<<ComboboxSelected>>', self.select_value)
combo.grid(column=0, row=0) combo.grid(column=0, row=0)
Button(self, text="装载", width=10, command=self.load).grid(column=1, row=0, padx=5) Button(self, text="装载", width=9, command=self.load).grid(column=1, row=0, padx=5)
def create_tree_widget(self): def create_tree_widget(self):
# 创建画布,用于展示装载后的数据 # 创建画布,用于展示装载后的数据
frame = Frame(self, width=50, height=180) frame = Frame(self, width=50, height=250)
frame.grid(row=1, column=0, columnspan=2, sticky=NSEW, pady=5) frame.grid(row=1, column=0, columnspan=2, sticky=NSEW, pady=5)
frame.grid_columnconfigure(0, weight=1) frame.grid_columnconfigure(0, weight=1)
frame.grid_rowconfigure(0, weight=1) frame.grid_rowconfigure(0, weight=1)
@ -83,12 +84,13 @@ class LoadElement(Frame):
self.create_tree_widget() self.create_tree_widget()
class FormElement(Frame): class FormElement(Frame):
def __init__(self, master, node): def __init__(self, master, node):
super().__init__(master) super().__init__(master)
self.node = node self.node = node
self.data = [] self.data = []
self.conn = pymysql.connect(user="root", password="cyh0110", database="SCT", host="127.0.0.1", port=3306) self.conn = pymysql.connect(user="root", password="123123", database="stu_sys", host="127.0.0.1", port=3306)
self.font_style = tkFont.Font(family="Lucida Grande", size=20) self.font_style = tkFont.Font(family="Lucida Grande", size=20)
self.button_font = tkFont.Font(family="Lucida Grande", size=10) self.button_font = tkFont.Font(family="Lucida Grande", size=10)
self.table_names = [] self.table_names = []
@ -100,14 +102,6 @@ class FormElement(Frame):
self.select_table(None) self.select_table(None)
self.create_treeview(pd.read_sql(self.default_query_sql, con=self.conn)) self.create_treeview(pd.read_sql(self.default_query_sql, con=self.conn))
def get_table_names(self):
"""
获取该数据库下所有的表
:return:
"""
data_frame = pd.read_sql("show tables", self.conn)
self.table_names = [i[0] for i in data_frame.values.tolist()]
def select_table(self, event): def select_table(self, event):
""" """
当下拉框选择表之后触发该函数并将tableName改为选择的tableName 当下拉框选择表之后触发该函数并将tableName改为选择的tableName
@ -124,13 +118,27 @@ class FormElement(Frame):
self.sql_text.delete(0.0, END) self.sql_text.delete(0.0, END)
self.sql_text.insert(1.0, f"SELECT * FROM {self.table_name}") self.sql_text.insert(1.0, f"SELECT * FROM {self.table_name}")
def get_table_names(self):
"""
获取该数据库下所有的表
:return:
"""
data_frame = pd.read_sql("show tables", self.conn)
self.table_names = [i[0] for i in data_frame.values.tolist()]
def get_field_names(self): def get_field_names(self):
""" """
获取所有数据表的所有字段 获取所有数据表的所有字段
:return: 返回表内的所有字段 :return: 返回表内的所有字段
""" """
data_frame = pd.read_sql(f"desc {self.table_name}", self.conn) data_frame = pd.read_sql(f"desc {self.table_name}", self.conn)
return data_frame["Field"].tolist() """
field_names = []
for field in data_frame["Field"]:
field_names.append(f"{self.table_name}.{field}")
return field_names
"""
return data_frame["Field"].values.tolist()
def create_form(self): def create_form(self):
""" """
@ -139,7 +147,7 @@ class FormElement(Frame):
""" """
self.select_table_name = StringVar() self.select_table_name = StringVar()
self.select_table_name.set(self.table_name) self.select_table_name.set(self.table_name)
combo = ttk.Combobox(self, width=10, height=1, textvariable=self.select_table_name, font=self.font_style, combo = ttk.Combobox(self, width=8, height=1, textvariable=self.select_table_name, font=self.font_style,
style="TCombobox") style="TCombobox")
combo["values"] = self.table_names combo["values"] = self.table_names
combo.bind('<<ComboboxSelected>>', self.select_table) combo.bind('<<ComboboxSelected>>', self.select_table)
@ -158,7 +166,7 @@ class FormElement(Frame):
checkbutton_var = IntVar() checkbutton_var = IntVar()
checkbutton = Checkbutton(self, text='', variable=checkbutton_var) checkbutton = Checkbutton(self, text='', variable=checkbutton_var)
checkbutton.grid(row=i + 1, column=j * 2, padx=5, pady=5, sticky=E) checkbutton.grid(row=i + 1, column=j * 2, padx=5, pady=5, sticky=E)
entry = Entry(self, font=self.font_style) entry = Entry(self, font=self.font_style,width=12)
entry.grid(row=i + 1, column=j * 2 + 1, padx=5, pady=5, sticky=W) entry.grid(row=i + 1, column=j * 2 + 1, padx=5, pady=5, sticky=W)
self.form_content[field_name] = (entry, checkbutton_var, label, checkbutton) self.form_content[field_name] = (entry, checkbutton_var, label, checkbutton)
style = Style() style = Style()
@ -166,7 +174,7 @@ class FormElement(Frame):
Button(self, text="构造SQL", command=self.make_sql, width=8, style="custom.TButton" Button(self, text="构造SQL", command=self.make_sql, width=8, style="custom.TButton"
).grid(column=8, row=0, rowspan=8) ).grid(column=8, row=0, rowspan=8)
self.sql_text = Text(self, height=3, font=self.font_style, width=75) self.sql_text = Text(self, height=3, font=self.font_style, width=46)
self.sql_text.grid(row=8, column=0, rowspan=2, columnspan=7, sticky=W) self.sql_text.grid(row=8, column=0, rowspan=2, columnspan=7, sticky=W)
Button(self, text="\n执行SQL\n", command=self.execute_sql, width=8, Button(self, text="\n执行SQL\n", command=self.execute_sql, width=8,
).grid(column=8, row=8, rowspan=2, padx=10) ).grid(column=8, row=8, rowspan=2, padx=10)
@ -195,8 +203,11 @@ class FormElement(Frame):
frame.grid(row=10, column=0, columnspan=10, rowspan=5, sticky=NSEW, pady=10) frame.grid(row=10, column=0, columnspan=10, rowspan=5, sticky=NSEW, pady=10)
frame.grid_columnconfigure(0, weight=1) frame.grid_columnconfigure(0, weight=1)
frame.grid_rowconfigure(0, weight=1) frame.grid_rowconfigure(0, weight=1)
columns = data_frame.keys().tolist() unique_columns = data_frame.columns[~data_frame.columns.duplicated()]
tree = ttk.Treeview(frame, columns=columns, show="headings", style="mystyle.Treeview", height=50, selectmode=BROWSE) columns = unique_columns.tolist()
# 获取查询的列名,将其转换为列表
tree = ttk.Treeview(frame, columns=columns, show="headings", style="mystyle.Treeview", height=50,
selectmode=BROWSE)
style = ttk.Style() style = ttk.Style()
style.configure("Treeview.Heading", font=self.font_style) style.configure("Treeview.Heading", font=self.font_style)
style.configure("Treeview", rowheight=30, font=self.font_style, borderwidth=1) style.configure("Treeview", rowheight=30, font=self.font_style, borderwidth=1)
@ -229,7 +240,6 @@ class FormElement(Frame):
self.sql_text.delete(0.0, END) self.sql_text.delete(0.0, END)
self.sql_text.insert(1.0, sql) self.sql_text.insert(1.0, sql)
class StuSys_X4(BaseWindow): class StuSys_X4(BaseWindow):
def __init__(self, master, attr): def __init__(self, master, attr):
super().__init__(master, attr) super().__init__(master, attr)
@ -239,6 +249,8 @@ class StuSys_X4(BaseWindow):
def create_left_plate(self): def create_left_plate(self):
self.left_frame = Frame(self.master, height=self.attr["height"]) self.left_frame = Frame(self.master, height=self.attr["height"])
self.right_frame = Frame(self.master, height=self.attr["height"])
self.right_frame.pack(side=RIGHT, anchor=NW, pady=10, padx=10)
self.left_frame.pack(side=LEFT, anchor=NW, pady=10, padx=10) self.left_frame.pack(side=LEFT, anchor=NW, pady=10, padx=10)
load_element1 = LoadElement(self.left_frame, "student") load_element1 = LoadElement(self.left_frame, "student")
load_element1.pack(side=TOP, pady=10) load_element1.pack(side=TOP, pady=10)
@ -246,9 +258,9 @@ class StuSys_X4(BaseWindow):
load_element2.pack(side=TOP, pady=10) load_element2.pack(side=TOP, pady=10)
load_element2 = LoadElement(self.left_frame, "sc") load_element2 = LoadElement(self.left_frame, "sc")
load_element2.pack(side=TOP, pady=10) load_element2.pack(side=TOP, pady=10)
message_frame = Frame(self.left_frame, width=50, height=180) message_frame = Frame(self.right_frame, width=50, height=180)
message_frame.pack(side=TOP, pady=10, fill=BOTH) message_frame.pack(side=TOP, pady=10, fill=BOTH)
self.canvas = Canvas(message_frame, width=50, height=180, bg="#f0f5fa") self.canvas = Canvas(message_frame, width=280, height=1000, bg="#f0f5fa")
self.canvas.configure(background="#f0f5fa") self.canvas.configure(background="#f0f5fa")
y_scrollbar = Scrollbar(message_frame, orient="vertical", command=self.canvas.yview) y_scrollbar = Scrollbar(message_frame, orient="vertical", command=self.canvas.yview)
x_scrollbar = Scrollbar(message_frame, orient="horizontal", command=self.canvas.xview) x_scrollbar = Scrollbar(message_frame, orient="horizontal", command=self.canvas.xview)
@ -263,7 +275,8 @@ class StuSys_X4(BaseWindow):
for message in self.message: for message in self.message:
index = self.message.index(message) index = self.message.index(message)
self.canvas.create_text(0, 20 * index, anchor="nw", self.canvas.create_text(0, 20 * index, anchor="nw",
text=message.split("-")[0], font=("", 15), fill="black" if message.split("-")[-1] == "true" else "red") text=message.split("-")[0], font=("", 15),
fill="black" if message.split("-")[-1] == "true" else "red")
self.canvas.update() self.canvas.update()
self.canvas.configure(scrollregion=self.canvas.bbox("all")) self.canvas.configure(scrollregion=self.canvas.bbox("all"))
@ -273,16 +286,15 @@ class StuSys_X4(BaseWindow):
form_element = FormElement(self.right_frame, self) form_element = FormElement(self.right_frame, self)
form_element.pack(side=TOP, pady=10, anchor=NE) form_element.pack(side=TOP, pady=10, anchor=NE)
if __name__ == '__main__': if __name__ == '__main__':
""" """
minty, lumen, sandstone, yeti, pulse, united, morph, journal, darkly, superhero, solar minty, lumen, sandstone, yeti, pulse, united, morph, journal, darkly, superhero, solar
cyborg, vapor, simplex, cerculean, cyborg, vapor, simplex, cerculean,
""" """
from pycallgraph2 import PyCallGraph # from pycallgraph2 import PyCallGraph
from pycallgraph2.output import GraphvizOutput # from pycallgraph2.output import GraphvizOutput
from pycallgraph2 import Config # from pycallgraph2 import Config
from pycallgraph2 import GlobbingFilter # from pycallgraph2 import GlobbingFilter
style = "morph" style = "morph"
root = Window(themename=style) root = Window(themename=style)
screenwidth = root.winfo_screenwidth() screenwidth = root.winfo_screenwidth()
@ -290,23 +302,14 @@ if __name__ == '__main__':
root.columnconfigure(0, weight=1) root.columnconfigure(0, weight=1)
root.rowconfigure(1, weight=1) root.rowconfigure(1, weight=1)
root_attr = { root_attr = {
"width": screenwidth * 0.83, "width": screenwidth * 0.88,
"height": screenheight * 0.85, "height": screenheight * 0.90,
} }
alignstr = '%dx%d+%d+%d' % (root_attr['width'], root_attr['height'], (screenwidth - root_attr['width']) / 2, alignstr = '%dx%d+%d+%d' % (root_attr['width'], root_attr['height'], (screenwidth - root_attr['width']) / 2,
(screenheight - root_attr['height']) / 2) (screenheight - root_attr['height']) / 2)
root.geometry(alignstr) root.geometry(alignstr)
root.resizable(width=False, height=False) root.resizable(width=False, height=False)
output = GraphvizOutput(font_size=30)
output.output_file = "basic.png" app = StuSys_X4(root, root_attr)
output.group_font_size = 40 ttk.Style().configure("TButton", font="-size 18")
config = Config()
config.trace_filter = GlobbingFilter(include=[
'StuSys_X4.*',
'FormElement.*',
'LoadElement.*',
])
with PyCallGraph(output=output, config=config):
app = StuSys_X4(root, root_attr)
ttk.Style().configure("TButton", font="-size 18")
root.mainloop() root.mainloop()

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

Loading…
Cancel
Save