0 pythonでfoobarのalternativeを作る 9

DBのGUIビューア

  • 190516 開発
    wxでGUI化した、結構簡単だった(・∀・)ノ
    データ取得の部分はコンソールを移しただけだから当然か
import wx ,sqlite3
import sqlviewer_ui


class MyFrame( sqlviewer_ui.MyFrame1 ):
    def __init__( self, parent ):
        sqlviewer_ui.MyFrame1.__init__( self, parent )
        self.db=r'フルパス'
        self.btn_refresh(0)

    def btn_refresh( self, event ):
        conn = sqlite3.connect( self.db )
        c = conn.cursor()
        c.execute("select * from sqlite_master where type='table'")
        table=c.fetchone()[1] # 最初のテーブル名
        des=c.execute('select * from {}'.format(table) ).description
        colis=[item[0] for item in des]
        i=0
        nonull=[]
        null=[]
        for col in colis :
            res=c.execute( 'select count(*) from {} where {} IS NULL'.format(table,colis[i]) )
            null.append(res.fetchall()[0][0])
            res=c.execute( 'select count(*) from {} where {} IS NOT NULL'.format(table,colis[i]) )
            nonull.append(res.fetchall()[0][0])
            i+=1

        self.m_staticText1.SetLabel('DATABASE -- {}'.format(self.db))
        self.m_staticText2.SetLabel('   Table -- {}'.format(table))

        all=[colis,nonull,null]
        line=''
        for i in all:
            for ii in i :
                line += '{:>10}'.format(ii)
            line += '\r\n'
        self.m_textCtrl1.SetValue(line)

        self.table=table
        c.close()
        conn.close()

    def btn_go( self, event ):
        col=self.m_textCtrl2.GetValue()
        exp=self.m_textCtrl3.GetValue()
        num=self.m_textCtrl4.GetValue()
        line='select '+col+' from '+self.table+' where '+exp+' limit '+num
        self.m_listBox1.Append(line)

        conn = sqlite3.connect( self.db )
        c = conn.cursor()
        for l in c.execute(line):
            self.m_listBox1.Append(l[0]) # タプルの最初の要素だけ
            print(l)
        c.close()
        conn.close()

    def btn_bye( self, event ):
        quit()


if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame(None)
    frame.Show(True)
    app.MainLoop()
  • wx のコードもいるか??

  • select コマンドを発行出来るようにしたが、コマンドは基本文字列を作るだけなので結構簡単だった
    pythonのプログラムの中で書く時にプレースホルダーにして変数はタプルにして、といった部分がややこしいんだなあ

  • まだselectの戻り値をうまく取得できていない。タプルで返ってくる最初の要素だけを表示している(画像は取得できたときのだ、次回だな)。やはりListViewにしてカラムに表示した方が簡単なのかな。wxのListViewは使ったこと無い、調べるか(-_-)

  • この感じだとinsertupdateも簡単そうだけどそこまでは必要ないか??作りたかったのはビューアで幅広のユーティリティじゃないからなあ

0

メモを他の人に見せる

このメモを見せたい人に、このURL(今開いているページのURLです)を教えてあげてください

コメント(0)

  • someone

  • someone