> 文档中心 > Python建立sqlit数据库,写入数据软件设计

Python建立sqlit数据库,写入数据软件设计

建立一个sqlit数据库,保存我们的图书信息,建立我们的数据表在这里插入图片描述
软件界面如下:
在这里插入图片描述
我们需要借助wxpython来建立我们的软件GUI界面,建立一个类(对象)来对我们的数据进行写入,将我们输入的信息写入到我们的数据库中,从而实现我们的目标
具体代码如下:

import sqlite3 as sqimport wxAPP_TITLE = u'Database entry'class Book(object):    def __init__(self,name="",author="",quantity=0): self.setName(name)    #Initialization function type self.setAuthor(author)    #Initialization function type self.setQuantity(quantity)     #Initialization function type    def setName(self,name): if not isinstance(name,str):     print('name must be string.')     return self.__name = name    def setAuthor(self,author): if not isinstance(author,str):     print('author must be string.')     return self.__author = author    def setQuantity(self,quantity): if not isinstance(quantity,int):     print('quantity must be integer.')     return self.__quantity = quantity    def show(self): print(self.__name) print(self.__author) print(self.__quantity)class Computerbook(Book):    def __init__(self,name='',author='',quantity=1,computerField="python programming"): self.setName(name)     #Initialization function type self.setAuthor(author)    #Initialization function type self.setQuantity(quantity)     #Initialization function type self.setComputerField(computerField)    #Initialization function type self.insertDB()    def setName(self, name): super(Computerbook, self).setName(name)    def setAuthor(self,author): super(Computerbook, self).setAuthor(author)    def setQuantity(self,quantity): super(Computerbook, self).setQuantity(quantity)    def setComputerField(self,computerField): if not isinstance(computerField,str):     print('computerField must be string.')     return self.__computerField = computerField    def insertDB(self): try:     coon=sq.connect("ComputerbookDB")     cursor=coon.cursor()     sql='insert into ComputerBookTable(name,author,quantity,computerField) values(?,?,?,?)'     table=(self._Book__name,self._Book__author,self._Book__quantity,self.__computerField)     cursor.execute(sql,table)     coon.commit()     coon.close()     print("Database imported successfully") except:     print("error")    def show(self): super(Computerbook, self).show() print(self.__computerField)class mainFrame(wx.Frame):    def __init__(self,parent): wx.Frame.__init__(self,parent,-1,APP_TITLE) self.SetSize((650,450))     #Determine window size wx.StaticText(self, -1, u'Name', pos=(0, 50), size=(250, -1), style=wx.ALIGN_RIGHT) wx.StaticText(self, -1, u'Author', pos=(0, 100), size=(250, -1), style=wx.ALIGN_RIGHT) wx.StaticText(self, -1, u'Quantity(please enter an integer)', pos=(0, 150), size=(250, -1), style=wx.ALIGN_RIGHT) wx.StaticText(self, -1, u'ComputerField', pos=(0, 200), size=(250, -1), style=wx.ALIGN_RIGHT) self.tc1 = wx.TextCtrl(self, -1, '', pos=(270, 50), size=(200, -1), name='TC01', style=wx.ALIGN_LEFT) self.tc2 = wx.TextCtrl(self, -1, '', pos=(270, 100), size=(200, -1), name='TC02', style=wx.ALIGN_LEFT) self.tc3 = wx.TextCtrl(self, -1, '', pos=(270, 150), size=(200, -1), name='TC03', style=wx.ALIGN_LEFT) self.tc4 = wx.TextCtrl(self, -1, '', pos=(270, 200), size=(200, -1), name='TC04', style=wx.ALIGN_LEFT) btn_mea = wx.Button(self, -1, u'Insert', pos=(250, 300), size=(100, 30)) btn_mea.Bind(wx.EVT_LEFT_DOWN, self.Do) self.tc1.Bind(wx.EVT_TEXT, self.EvtText_1) self.tc2.Bind(wx.EVT_TEXT, self.EvtText_2) self.tc3.Bind(wx.EVT_TEXT, self.EvtText_3) self.tc4.Bind(wx.EVT_TEXT, self.EvtText_4) self.arr4=""    def EvtText_1(self, evt): '''Input box event function''' self.arr1="" obj = evt.GetEventObject() objName = obj.GetName() text = evt.GetString() if objName == 'TC01':     self.arr1+=text    def EvtText_2(self, evt): '''Input box event function''' self.arr2="" obj = evt.GetEventObject() objName = obj.GetName() text = evt.GetString() if objName == 'TC02':     self.arr2+=text    def EvtText_3(self, evt): '''Input box event function''' self.arr3="" obj = evt.GetEventObject() objName = obj.GetName() text = evt.GetString() if objName == 'TC03':     self.arr3+=text    def EvtText_4(self, evt): '''Input box event function''' self.arr4="" obj = evt.GetEventObject() objName = obj.GetName() text = evt.GetString() if objName == 'TC04':     self.arr4+=text    def Do(self,evt): if len(self.arr4)==0:     doing=Computerbook(self.arr1,self.arr2,int(self.arr3)) else:     doing=Computerbook(self.arr1,self.arr2,int(self.arr3),self.arr4) doing.show()class mainApp(wx.App):    def OnInit(self): self.SetAppName(APP_TITLE) self.Frame = mainFrame(None) self.Frame.Show() return Truedef main():    app = mainApp()    app.MainLoop()if __name__=='__main__':    main()

以上就是我们本期的全部内容

K歌软件