一、项目说明:
本次通过实现一个小的功能模块对Python GUI进行实践学习。项目来源于软件制造工程的作业。记录在这里以复习下思路和总结编码过程。所有的源代码和文件放在这里:
链接: https://pan.baidu.com/s/1qXGVRB2 密码: 4a4r
内置四个文件,分别是ora.sql, dataBaseOpr.py, guiPy.py, test.py
二、效果预览:
主界面
新增界面(更新界面一致)
功能很简单,就是做一张表的增删改查,借此简单的熟悉下python,前几天才看了看相关的语法。
三、环境说明:
数据库采用oracle12c,使用命令行进行操作。Python版本为3.6.2,命令行+Pycharm社区版2017.1.4。Python库使用了
cx_Oracle: 连接oracle数据库
tkinter: 简单入门的GUI库
cx_Oracle库的安装我直接使用IDE自带的包管理进行下载安装的,tkinter是Python3.2以后自带的标准库,后面会讲。
四、编码过程实现:
1、数据库表实现(ora.sql):
conn username/pass 根据本机的用户名和密码修改,后面的数据库连接统一都用我自己密码,不再赘述。
为了简化Python代码和实践sql能力,写了两个简单的存储过程,分别是插入和更新,成功创建后只需调用存储过程和传递参数列表即可。代码详情在ora.sql中。
代码折叠:
1 conn c##bai/bai1232 --建表
3 create or replace tablegroupinfo (4 no varchar(12) not null,5 name varchar(20),6 headername varchar(20),7 tel varchar(15),8 constraint pk_groupinfo primary key(no));9
10 --创建过程,直接传入参数即可插入
11 create or replace procedureinsert_groupinfo12 (no groupinfo.no%type,13 name groupinfo.name%type,14 headername groupinfo.headername%type,15 tel groupinfo.tel%type16 )17 is
18 begin
19 insert into groupinfo values(no,name,headername,tel);20 commit;21 end;22
23 --创建过程,直接传入参数即可完成更新,第一个字段为原纪录no。必须有。
24 create or replace procedureupdate_groupinfo25 (oldno groupinfo.no%type,26 no groupinfo.no%type,27 name groupinfo.name%type,28 headername groupinfo.headername%type,29 tel groupinfo.tel%type30 )31 is
32 n_no groupinfo.no%type;33 n_name groupinfo.name%type;34 n_headername groupinfo.headername%type;35 n_tel groupinfo.tel%type;36 grow groupinfo%rowtype;37 ex_oldnoisnull exception;38 begin
39 select * into grow from groupinfo g where g.no=oldno;40 if oldno is null or grow.no is null then
41 raise ex_oldnoisnull;42 end if;43 if no is null then
44 n_no:=oldno;45 else
46 n_no:=no;47 end if;48 if name is null then
49 n_name:=grow.name;50 else
51 n_name:=name;52 end if;53 if headername is null then
54 n_headername:=grow.headername;55 else
56 n_headername:=headername;57 end if;58 if tel is null then
59 n_tel:=grow.tel;60 else
61 n_tel:=tel;62 end if;63 --dbms_output.put_line(n_no||n_name||n_headername||n_tel);
64 update groupinfo g set g.no = n_no, g.name = n_name, g.headername = n_headername, g.tel = n_tel where g.no =oldno;65 commit;66 exception67 when ex_oldnoisnull then
68 dbms_output.out_line('选择的行不存在')69 end;
ora.sql
2、数据库操作类(dataBaseOpr.py):
先贴源码,折叠起来:
1 #!/usr/bin/env python
2 #encoding: utf-8
3 """
4 :author: xiaoxiaobai5
6 :contact: 865816863@qq.com7
8 :file: dataBaseOpr.py9
10 :time: 2017/10/3 12:0411
12 :@Software: PyCharm Community Edition13
14 :desc: 连接oracle数据库,并封装了增删改查全部操作。15
16 """
17 importcx_Oracle18
19
20 classOracleOpr:21
22 def __init__(self, username='c##bai', passname='bai123