PyQt5는python3.4와 sqlit3를 결합하여 작성한 작은 프로그램 원본 코드

본 프로젝트의 수요 첫 번째 단계는 excel에서 주문 데이터를 데이터베이스에 가져온 다음에 회원 이름에 따라 이 회원 이름이 진행하는 모든 거래 주문서를 조회하는 것입니다. 조회 결과는 내보내기를 지원합니다. 내보내기 형식은 excel입니다. 데이터 양이 적어서pyscopg2로postgresql를 조작하려다가 포기했습니다. 한 달에 1w개의 데이터만 가지고 있기 때문에 자체 sqlit3를 선택했습니다.
본고에서 사용한 라이브러리는xlrdxlwtsqlit3pyqt5
세 가지 종류로 나뉘는데 하나는 excel 데이터 조작 클래스입니다. 하나는 excel 데이터의 읽기와 쓰기를 포함하고 하나는db 조작 클래스입니다. 기본적인 연결 라이브러리 창설표와 조회 삽입을 포함하고 다른 하나는 인터페이스 클래스입니다. 인터페이스에 연결된 조작과 인터페이스 데이터를 가져옵니다.
원본은 다음과 같다.
      
'''
   omsData.py
   @author tianrui
   @date 2015-11-25
'''
 
#coding=utf-8
import xlrd
import os
import xlwt

def DriverDir(dirPath):
    filePathlist = [];
    for parent,filenames in os.walk(dirPath):
        for filename in filenames:
            if((filenames.find('.xls') != -1) and (filenames.find('.xlsx') != -1)):
                filename = parent + '\\' + filename
                filePathlist.append(filename)
    ''' '''            
    return filePathlist

class omsExcel:
    
    def readDirData(self, dirPath):
        ''' xls,xlsm    '''
        fileList = DriverDir(dirPath)
        dataList = []
        for filePath in fileList:
            singleDataList = self.readFileData(filePath)
            dataList.append(singleDataList)
        '''datalist[0][0][0]'''   
        return dataList
     
    def readFileData(self, filePath):
        ''' '''  
        listData = []
        wkBook = xlrd.open_workbook(filePath)
        for wkSht in wkBook.sheets():
            nrows = wkSht.nrows
            ncols = wkSht.ncols
            ''' '''
            for rownum in range(1, nrows):  
                row = wkSht.row_values(rownum)
                if row:
                    rowData = {}
                    for col in range(0, ncols):
                        rowData[col] = row[col]
                        
                    listData.append(rowData)
        return listData
    
    
    def writeFileData(self, filePath, dataList):
        ''' '''
        writeBk = xlwt.Workbook()
        writeSht = writeBk.add_sheet(" ")
        ''' , workbook  , '''
        for wb  in range(0, len(dataList)):
            for row in range(0, len(dataList[0])):
                for col in range(0, len(dataList[0][0])):
                    writeSht.write(row, col, dataList[wb][row][col])
        writeBk.save(filePath)

    
#coding=utf-8
import sqlite3
from omsData import omsExcel

class dbOperator:
        
    def __init__(self):
        print("DBoperator")
    
    def ConnectDb(self):
        try:
            print("connect")
            self.conn = sqlite3.connect("omsDB.db")
        except Exception as ex:
            print(ex)
        else:
            print("create")
            self.createTable()
    
    ''' '''         
    def createTable(self):
        self.conn.execute('''CREATE TABLE IF NOT EXISTS t_oms_data
                        ( orderID         text           primary key not null,
                          accountName     text            ,
                          zhifubaoName    text            ,
                          payableMoney    text            ,
                          postMoney       text            ,
                          zhufuPoint      text            ,
                          totalMoney      text            ,
                          backPoint       text            ,
                          realMoney       text            ,
                          realzhifuPoint  text            ,
                          orderState      text            ,
                          customMsg       text            ,
                          customName      text            ,
                          recvAddress     text            ,
                          postWay         text            ,
                          phoneNum        text            ,
                          telphoneNum     text            ,
                          odCreateTime    text            ,
                          odpayTime       text            ,
                          goodsTitle      text            ,
                          goodsType       text            ,
                          logisticsNum    text            ,
                          logisticsCmy    text            ,
                          odRemark        text            ,
                          goodsTotal      text            ,
                          storeID         text            ,
                          storeName       text            ,
                          odCloseReason   text            ,
                          serverMoney     text            ,
                          customSerMoney  text            ,
                          invoiceName     text            ,
                          isTelphonOD     text            ,
                          odPartInfo      text            ,
                          earnestRank     text            ,
                          modifySku       text            ,
                          modifyAdd       text            ,
                          exceptInfo      text            ,
                          tianmaoCard     text            ,
                          jifenbaoCart    text            ,
                          isO2O           text            );''')
    
    def putValue(self, datalist, singleFile = True):
        sqlStr = ''
        realList = []
        if singleFile == True:
            realList.append(datalist)
        else:
            realList = datalist
        print(realList)
        for wb in range(0, len(realList)):
            for row in range(0, len(realList[0])):
                sqlStr = "INSERT INTO t_oms_data VALUES("
                for col in range(0, len(realList[0][0])):
                    newStr = str(realList[wb][row][col])
                    if(newStr.find("'") != -1):
                        newStr = newStr.replace("'","")
                    tmpStr = "'%s'" % (newStr)                           
                    if col != len(realList[0][0]) - 1:
                        tmpStr += ","
                    sqlStr += tmpStr
                sqlStr += ");"
                ''' '''   
                try:     
                    print(sqlStr)
                    cur = self.conn.cursor()
                    cur.execute(sqlStr)
                except Exception as ex:
                    print(ex)
                    continue
                else:
                    self.conn.commit()
                      
    def getFindValue(self, sqlStr):
        #sqlStr = "select * from t_oms_data where accountName = '%s'" %(accountName)
        cur = self.conn.cursor()
        cur.execute(sqlStr)
        rows = cur.fetchall()
        cur.close()
        ''' '''
        return rows
    
    def closeDb(self):
        if(self.conn == None):
            self.conn.close()

''' test
conn = sqlite3.connect("test.db")   
cur = conn.cursor()
cur.execute("select * from t1")

res = cur.fetchall()

print(res[0][0])'''

또 하나의 UI 조작 글자수 제한은 올릴 수 없습니다.

좋은 웹페이지 즐겨찾기