Skip to content

Commit

Permalink
Tweaking #266
Browse files Browse the repository at this point in the history
Working on using DB grid
  • Loading branch information
jarvisteach committed Jan 27, 2018
1 parent 369eb0c commit 259a73b
Showing 1 changed file with 77 additions and 13 deletions.
90 changes: 77 additions & 13 deletions examples/issues/issue266.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,76 @@
import sys
sys.path.append("../../")

def delRow(val):
print(val)
DB_NAME = "issue266.db"
types = ["NULL", "INTEGER", "REAL", "TEXT", "BLOB"]

def addRow(val):
print(val)
def makeTable():
# app.hideSubWindow("Make Table")
fields = app.getAllEntries()
types = app.getAllOptionBoxes()
prim = app.radio("PRIMARY")
tbName = app.entry("Table Name")

def changeDb(val):
print(val)
app.replaceDbGrid("projects", "issue266.db", app.list("table")[0])
data = "CREATE TABLE IF NOT EXISTS " + tbName + "("


counter = 0
while True:
try:
data += fields["field"+str(counter)] + " " + types["type"+str(counter)]
if app.radio("PRIMARY") == "primary"+str(counter):
data += " PRIMARY KEY"
data += ", "
counter += 1
except:
break
data = data[:-2]
data += ");"
runSql(data)
app.changeOptionBox("table", getTables())
app.removeSubWindow("Make Table")

def runSql(sql):
print(sql)
data = []
import sqlite3
with sqlite3.connect(DB_NAME) as conn:
c = conn.cursor()
c.execute(sql)
for r in c:
data.append(r)
return data

def genRows():
with app.subWindow("Make Table"):
row = app.gr()
with app.labelFrame(app.entry("Table Name")):
for i in range(int(app.entry("Num Fields"))):
app.entry("field"+str(i), row=row, column=0)
app.optionBox("type"+str(i), types, row=row, column=1)
app.radio("PRIMARY", "primary"+str(i), row=row, column=2)
row += 1

app.setFocus("field0")

app.button("GO", makeTable)

def showMakeTable():
with app.subWindow("Make Table"):
app.entry("Table Name", label=True, focus=True, colspan=3)
app.entry("Num Fields", label=True, kind="numeric", submit=genRows, colspan=3)
app.showSubWindow("Make Table")

def changeDb():
app.replaceDbGrid("projects", DB_NAME, app.optionBox("table"))

def getTables():
query = "SELECT distinct tbl_name from sqlite_master order by 1"
rows = runSql(query)
tables = []
for r in rows:
tables.append(r[0])
return tables

from appJar import gui

Expand All @@ -20,7 +81,7 @@ def setup():
proj = ''' INSERT INTO projects(name,begin_date,end_date) VALUES(?,?,?) '''
task = ''' INSERT INTO tasks(name,priority,status_id,project_id,begin_date,end_date) VALUES(?,?,?,?,?,?) '''

with sqlite3.connect("issue266.db") as conn:
with sqlite3.connect(DB_NAME) as conn:
c = conn.cursor()
c.execute( """ CREATE TABLE IF NOT EXISTS projects (
id integer PRIMARY KEY,
Expand All @@ -45,9 +106,12 @@ def setup():
c.execute(task, ('Analyze the requirements of the app', 1, 1, project_id, '2015-01-01', '2015-01-02'))
c.execute(task, ('Confirm with user about the top requirements', 1, 1, project_id, '2015-01-03', '2015-01-05'))

with gui("DB Demo", "800x400") as app:
app.setStretch("column")
app.label("title", "DB tester")
app.list("table", ["projects", "tasks"], change=changeDb, rows=2)
with gui("DB Demo", stretch="column", bg="DarkOrange") as app:
app.label("title", "DB tester", bg="orange", font={'size':20})
app.config(sticky="NE")
tables = getTables()
app.optionBox("table", tables, change=changeDb)
app.config(sticky="NEWS")
app.setStretch("both")
app.addDbGrid("projects", "issue266.db", "projects", action=delRow, addRow=addRow, showMenu=True)
app.addDbGrid("projects", DB_NAME, "projects")
app.button("NEW TABLE", showMakeTable)

0 comments on commit 259a73b

Please sign in to comment.