这里贴一个实现程序界面和业务逻辑分离的程序代码。这里说的主线程是UI线程,如果在这里面做逻辑处理的话,太长时间的处理会使主线程阻塞,表现就是界面假死。
直接贴代码吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# -*- coding: gbk -*- ''' python连接oracle ''' from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QObject, pyqtSignal import threading import time import cx_Oracle browser_msg = None #更新textBrowser内容 my_con = None #数据库连接 total_count = 0 #插入数据计数 class Ui_main(QtWidgets.QWidget): def __init__(self, parent=None): """ :param parent: """ super(Ui_main, self).__init__(parent) #界面初始化 self.setFixedSize(535, 338) self.setWindowTitle('oracle - python') self.groupBox = QtWidgets.QGroupBox(self) self.groupBox.setGeometry(QtCore.QRect(10, 20, 231, 191)) self.groupBox.setObjectName("groupBox") self.btnConnect = QtWidgets.QPushButton(self.groupBox) self.btnConnect.setGeometry(QtCore.QRect(130, 160, 75, 23)) self.btnConnect.setObjectName("btnConnect") self.layoutWidget = QtWidgets.QWidget(self.groupBox) self.layoutWidget.setGeometry(QtCore.QRect(20, 20, 191, 131)) self.layoutWidget.setObjectName("layoutWidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setObjectName("verticalLayout") self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.label = QtWidgets.QLabel(self.layoutWidget) self.label.setObjectName("label") self.horizontalLayout.addWidget(self.label) self.editUser = QtWidgets.QLineEdit(self.layoutWidget) self.editUser.setObjectName("editUser") self.horizontalLayout.addWidget(self.editUser) self.verticalLayout.addLayout(self.horizontalLayout) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.label_2 = QtWidgets.QLabel(self.layoutWidget) self.label_2.setObjectName("label_2") self.horizontalLayout_2.addWidget(self.label_2) self.editPsd = QtWidgets.QLineEdit(self.layoutWidget) self.editPsd.setObjectName("editPsd") self.horizontalLayout_2.addWidget(self.editPsd) self.verticalLayout.addLayout(self.horizontalLayout_2) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.label_3 = QtWidgets.QLabel(self.layoutWidget) self.label_3.setObjectName("label_3") self.horizontalLayout_3.addWidget(self.label_3) self.editServer = QtWidgets.QLineEdit(self.layoutWidget) self.editServer.setObjectName("editServer") self.horizontalLayout_3.addWidget(self.editServer) self.verticalLayout.addLayout(self.horizontalLayout_3) self.groupBox_2 = QtWidgets.QGroupBox(self) self.groupBox_2.setGeometry(QtCore.QRect(10, 220, 231, 101)) self.groupBox_2.setObjectName("groupBox_2") self.widget = QtWidgets.QWidget(self.groupBox_2) self.widget.setGeometry(QtCore.QRect(20, 30, 189, 22)) self.widget.setObjectName("widget") self.horizontalLayout_4 = QtWidgets.QHBoxLayout(self.widget) self.horizontalLayout_4.setContentsMargins(0, 0, 0, 0) self.horizontalLayout_4.setObjectName("horizontalLayout_4") self.label_4 = QtWidgets.QLabel(self.widget) self.label_4.setObjectName("label_4") self.horizontalLayout_4.addWidget(self.label_4) self.editThreads = QtWidgets.QLineEdit(self.widget) self.editThreads.setObjectName("editThreads") self.horizontalLayout_4.addWidget(self.editThreads) self.layoutWidget1 = QtWidgets.QWidget(self) self.layoutWidget1.setGeometry(QtCore.QRect(260, 20, 260, 301)) self.layoutWidget1.setObjectName("layoutWidget1") self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.layoutWidget1) self.verticalLayout_3.setContentsMargins(0, 0, 0, 0) self.verticalLayout_3.setObjectName("verticalLayout_3") self.verticalLayout_2 = QtWidgets.QVBoxLayout() self.verticalLayout_2.setObjectName("verticalLayout_2") self.label_5 = QtWidgets.QLabel(self.layoutWidget1) self.label_5.setObjectName("label_5") self.verticalLayout_2.addWidget(self.label_5) self.textStatus = QtWidgets.QTextBrowser(self.layoutWidget1) self.textStatus.setObjectName("textStatus") self.verticalLayout_2.addWidget(self.textStatus) self.verticalLayout_3.addLayout(self.verticalLayout_2) self.btnStart = QtWidgets.QPushButton(self.layoutWidget1) self.btnStart.setObjectName("btnStart") self.verticalLayout_3.addWidget(self.btnStart) self.groupBox.setTitle("oracle登录") self.btnConnect.setText("连接") self.label.setText("用户名:") self.label_2.setText("密码:") self.label_3.setText("服务器:") self.groupBox_2.setTitle("配置") self.label_4.setText("线程数:") self.label_5.setText("状态信息:") self.btnStart.setText("开始") self.editUser.setText('abcd') self.editPsd.setText('bacd') self.editServer.setText('abcd_local') self.editThreads.setText('1') global browser_msg #关联信号槽 self.btnConnect.clicked.connect(self.connect_oracle) self.btnStart.clicked.connect(self.insert_oracle) def connect_oracle(self): """ 连接数据库 """ self.ora_user = self.editUser.text() self.ora_psd = self.editPsd.text() self.ora_server = self.editServer.text() self.textStatus.setText('正在连接...') self.con = Connection(self.ora_user, self.ora_psd, self.ora_server) self.con.signal_connection.connect(self.slot_update_browser) self.con.start() def insert_oracle(self): """ 录入数据 """ self.insert = Insertion() self.insert.signal_insertion.connect(self.slot_update_browser) self.insert.start() def slot_update_browser(self): """ 更新textStatus内容 """ self.textStatus.setText(browser_msg) class Connection(QObject, threading.Thread): signal_connection = pyqtSignal() def __init__(self, ora_user, ora_psd, ora_server): super(Connection, self).__init__() self.ora_user = ora_user self.ora_psd = ora_psd self.ora_server = ora_server def run(self): global my_con global browser_msg sql_str = self.ora_user + '/' + self.ora_psd + '@' + self.ora_server try: my_con = cx_Oracle.connect(sql_str) except cx_Oracle.DatabaseError as exc: error, = exc.args browser_msg = 'Oracle-Error-Code:' + str(error.code) +'\nOracle-Error-Message:' + error.message self.signal_connection.emit() else: browser_msg = '连接oracle数据库成功...' self.signal_connection.emit() class Insertion(QObject, threading.Thread): signal_insertion = pyqtSignal() def __init__(self): super(Insertion, self).__init__() self.count = 0 def run(self): global my_con global browser_msg global total_count if my_con: self.cursor = my_con.cursor() while True: browser_msg = '正在录入第' + str(total_count+1) +'条数据...' self.signal_insertion.emit() total_count = total_count + 1 param_cufile = {'id':str(total_count), 'paperid':str(total_count)} self.id1 = total_count #录入ucrc_cufile、ucrc_loanopen、ucrc_loanrepayment表 try: self.cursor.execute("INSERT INTO ucrc_cufile values(:id,'N10155840H0002','林志玲','1',:paperid,'2',to_date('19930220', 'YYYYMMDD'),'2','3','05362545372','13424433317','02188567890','jerryz.xdu@gmail.com','深圳市宝安区西乡固戍社区93号94号95号46号','261523','广东省深圳市龙岗区葵新南路65号','深圳市葵涌高圳头股份合作公司','深圳市宝安区西乡盐田新一村6巷1号','518000','2','3','1','2','2009','500000','6225789187622234','招商银行','深圳市罗湖区黄贝街道凤凰路193号海珑华苑天高阁31F','518020','2','3','李冰冰','1','370623456754651234','深圳市科泰超进出口有限公司','13234567890','za001',to_date('20131010', 'YYYYMMDD'),'za001',to_date('20131021', 'YYYYMMDD'))", param_cufile) self.cursor.execute('commit') for i in range(0, 10): total_count = total_count + 1 self.id2 = total_count param_loanopen = {'id':str(total_count), 'cufile_id':str(self.id1), 'loanopen_id':str(total_count)} self.cursor.execute("INSERT INTO ucrc_loanopen values(:id,:cufile_id,'N10155840H0002',:loanopen_id,500000,500000,500000,500000,'23456',to_date('20081212', 'YYYYMMDD'),to_date('20120101', 'YYYYMMDD'),'24','///////////////////////*','1','0',to_date('20130202', 'YYYYMMDD'),'1','2','CNY','2','3','N10155840H000220130431112L4',to_date('20130202', 'YYYYMMDD'),'za001',to_date('20130202', 'YYYYMMDD'),'za001',to_date('20130202', 'YYYYMMDD'))", param_loanopen) self.cursor.execute('commit') for i in range(0, 10): total_count = total_count + 1 param_loanreapyment = {'id':str(total_count), 'loanrepyment_id':str(self.id2)} self.cursor.execute("INSERT INTO ucrc_loanrepayment values(:id,:loanrepyment_id,50000,50000,50000,50000,'12',to_date('20130202', 'YYYYMMDD'),to_date('20130202', 'YYYYMMDD'),20000,20000,0,0,0,0,0,0,0,0,'1','2','///////////////////////*',to_date('20130202', 'YYYYMMDD'),'1','N10155840H000220130431112L4',to_date('20130202', 'YYYYMMDD'),'za001',to_date('20130202', 'YYYYMMDD'),'za001',to_date('20130202', 'YYYYMMDD'),null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null)", param_loanreapyment) self.cursor.execute('commit') print('insert...') except cx_Oracle.DatabaseError as exc: error, = exc.args browser_msg = 'Oracle-Error-Code:' + str(error.code) +'\nOracle-Error-Message:' + error.message print(browser_msg) self.signal_insertion.emit() else: browser_msg = '未登录...' self.signal_insertion.emit() if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) w = Ui_main() w.show() sys.exit(app.exec_()) |
文章来自KENGINE | Kankanews.com