Welcome, guest | Sign In | My Account | Store | Cart

A GUI built around wxPython in a way that will allow you to add to this application other forms and functionalities. The main DialogBox is to create,in a fast way, Sqlite3 databases that are used for web or applications development. In order to work properly, save the script in a separate directory.There is also more; each time you create a database a text file named "CreationLog.txt" will be created (for the first time you use the application or if it is not there) and updated, describing when and where was your database created and with what SQL query. I am pleased to receive all the suggestions and improvements on this site or to my e-mail directly if this is convenient to you.

Python, 197 lines
  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
#!/usr/bin/env python

#-----------------------------------------------------------------------
# developer : toufic zaarour , Byblos-Lebanon

# for any suggestions or improvements, my gmail is : touficmc@gmail.com
#-----------------------------------------------------------------------

import wx
import wx.grid
import gettext
import os
import sqlite3
import datetime

cwd = os.path.abspath(os.curdir)

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.frame_1_menubar = wx.MenuBar()
        wxglade_tmp_menu = wx.Menu()
        wxglade_tmp_menu.Append(1, _("Create sqlite3 DataBase"), "", wx.ITEM_NORMAL)
        self.frame_1_menubar.Append(wxglade_tmp_menu,_("DataBase Configuration"))
        
        wxglade_tmp_menu = wx.Menu()
        wxglade_tmp_menu.Append(2, _("Message"), "", wx.ITEM_NORMAL)
        self.frame_1_menubar.Append(wxglade_tmp_menu,_("About"))

        self.SetMenuBar(self.frame_1_menubar)
        self.__set_properties()
        self.__do_layout()
        self.Bind(wx.EVT_MENU, self.open_dialog, id=1)
        self.Bind(wx.EVT_MENU,self.open_dialog1,id =2)


    def __set_properties(self):
        self.SetTitle(_("Sqlite3 Creator"))
        self.SetSize((555, 444))
        self.SetBackgroundColour(wx.Colour(255, 255, 255))


    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer_1)
        self.Layout()

    def open_dialog(self, event):
        MyDialog1(self).Show()

    def open_dialog1(self,event):
        wx.MessageBox("This App is made for you and for all developpers who use sqlite3 and want to create fast databases and data tables\n\nEnjoy...!")


class MyDialog1(wx.Dialog):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.label_27 = wx.StaticText(self, -1, _(" Create Your Data Tables :"))
        self.label_25 = wx.StaticText(self, -1, _(" File/Data/Table"))
        self.txtFileName = wx.TextCtrl(self, -1, "")
        self.txtDataName = wx.TextCtrl(self, -1, "")
        self.txtDataTable = wx.TextCtrl(self, -1, "")
        self.grid_1 = wx.grid.Grid(self, -1, size=(1, 1))
        self.bnt_add = wx.Button(self, -1, _("Add Column"))
        self.bnt_remove = wx.Button(self, -1, _("Remove Column"))
        self.bnt_create = wx.Button(self, -1, _("Create DataBase"))
        self.bnt_reset = wx.Button(self, -1, _("Reset Grid"))
        self.txtDataFileOutput = wx.TextCtrl(self, -1, "", style=wx.TE_READONLY)

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.cl_add_col, self.bnt_add)
        self.Bind(wx.EVT_BUTTON, self.clk_remove_col, self.bnt_remove)
        self.Bind(wx.EVT_BUTTON, self.clk_Create_db, self.bnt_create)
        self.Bind(wx.EVT_BUTTON, self.clk_reset_grid, self.bnt_reset)


    def __set_properties(self):
        self.SetTitle(_("DataBase Creator"))
        self.SetSize((555, 444))
        self.txtFileName.SetMinSize((100, 27))
        self.txtDataName.SetMinSize((100, 27))
        self.txtDataTable.SetMinSize((100, 27))
        self.grid_1.CreateGrid(1, 2)
        self.grid_1.SetColLabelValue(0, _("Name"))
        self.grid_1.SetColSize(0, 200)
        self.grid_1.SetColLabelValue(1, _("Type"))
        self.grid_1.SetColSize(1,200)
        self.bnt_add.SetMinSize((200, 29))
        self.bnt_remove.SetMinSize((200, 29))
        self.bnt_create.SetMinSize((200, 29))
        self.bnt_reset.SetMinSize((200, 29))
        self.txtDataFileOutput.SetMinSize((400, 30))

    def __do_layout(self):
        sizer_7 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_10 = wx.GridSizer(2, 2, 0, 0)
        grid_sizer_8 = wx.GridSizer(1, 4, 0, 0)
        sizer_7.Add(self.label_27, 0, 0, 0)
        grid_sizer_8.Add(self.label_25, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_8.Add(self.txtFileName, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_8.Add(self.txtDataName, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_8.Add(self.txtDataTable, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_7.Add(grid_sizer_8, 1, wx.EXPAND, 0)
        sizer_7.Add(self.grid_1, 1, wx.EXPAND, 0)
        grid_sizer_10.Add(self.bnt_add, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_10.Add(self.bnt_remove, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_10.Add(self.bnt_create, 0, wx.ALIGN_RIGHT, 0)
        grid_sizer_10.Add(self.bnt_reset, 0, 0, 0)
        sizer_7.Add(grid_sizer_10, 1, wx.EXPAND, 0)
        sizer_7.Add(self.txtDataFileOutput, 0, wx.EXPAND, 0)
        self.SetSizer(sizer_7)
        self.Layout()

    def cl_add_col(self, event):
        self.grid_1.AppendRows(1)
        event.Skip()

    def clk_remove_col(self, event):
        try:
            lst = self.grid_1.GetSelectedRows()[0]
            self.grid_1.DeleteRows(lst,1)
            event.Skip()
        except IndexError:
            wx.MessageBox("You Did Not Select Any Row To Delete")

    def clk_Create_db(self, event):
        try:
            DataColumnsList=[]
            DataTypeList=[]
            DataString=[]
            DataTableName=str(self.txtDataTable.Value)
            ConnectionString=cwd + "/"+(self.txtFileName.Value + "/" + self.txtDataName.Value + ".db")
            RowNum = self.grid_1.GetNumberRows()

            for i in range(0,RowNum):
                DataColumnsList.append(str(self.grid_1.GetCellValue(i,0)))
                DataTypeList.append(str(self.grid_1.GetCellValue(i,1)))
            
            for i in range(len(DataColumnsList)):
                DataString.append(DataColumnsList[i]+ " " + DataTypeList[i])
            
            elem= ",".join(DataString)
            CreateQuery = ("""CREATE TABLE """ + DataTableName + """(%s)"""%elem)

            if not os.path.isfile("CreationLog.txt"):
                f=open("CreationLog.txt","w")
            
            with open("CreationLog.txt","r+") as NewLog:
                OldLog=NewLog.read()
                NewLog.seek(0)
                NewLog.write(OldLog + "\nCreated on "+str(datetime.date.isoformat(datetime.datetime.now())) + " in '"+ ConnectionString + "' ,Used Query : "+ CreateQuery+"\n")
                
                
            if not os.path.exists(self.txtFileName.Value):
                os.makedirs(self.txtFileName.Value)
            else:
                wx.MessageBox("The Folder Already Exists, but you can add to it data tables!")
            
            cnn = sqlite3.connect(ConnectionString)
            cursor=cnn.cursor()
            cursor.execute(CreateQuery)
            cnn.commit
            cnn.close()
            
            self.txtDataFileOutput.Value=("A Data File Named "+self.txtDataName.Value+".db Was Created in "+ self.txtFileName.Value)
        
        except OSError:
            wx.MessageBox("The Grid Is Empty!")
        event.Skip()

    def clk_reset_grid(self, event):
        r=self.grid_1.GetNumberRows()
        
        for i in range(0,r):
            self.grid_1.DeleteRows(1,i)
        
        for c in range(0,2):
            self.grid_1.SetCellValue(0,c,"")
        
        self.txtFileName.Value=""
        self.txtDataName.Value=""
        self.txtDataTable.Value=""
        self.txtDataFileOutput.Value=""
        event.Skip()

if __name__ == "__main__":
    gettext.install("app")
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, wx.ID_ANY, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

All developers who design web, servers and desktop applications could use this simple tool to create graphically their sqlite databases in a simple and fast way in stead of writing code for it.

PS : I work on Linux Ubuntu but very easy to customize under Windows (just change the paths to the right format)

2 comments

toufic zaarour (author) 10 years, 7 months ago  # | flag

i just added a phone book that handles graphical interfacing with a data base and uses data grid view to show you the data content, you can also use the code above in order to create your sqlite3 phone book table it is here : http://code.activestate.com/recipes/578676-a-phone-book-gui-built-in-wxphython-connected-to-d/

Brandon leyrer 8 years, 6 months ago  # | flag

This is just mind blowing to see, I hope one day to achieve your skill set. I do have a question though if you don't mind. I would like to automate a file transfer for every past record that have been edited or modified from one file to another file. How about would I go about this task to add that functionality to your Gui's current functionality??