Here's an example of capturing a result set in a list control embedded
in a frame:
def SQLResultSet( parent, cmd, db, n):
"""
Returns a frame containing a list control which itself contains the results of
an SQL statement or returns nothing if the SQL statement did not yield any
results.
Arguments: parent - the parent window which will own the returned frame
cmd - string containing the SQL statement
db - the database connection
n - a string to be shown in the statusbar of the new frame
usage: w, fID = SQLResultSet( self, cmd, self.DataBase, "A message")
"""
c = db.cursor()
c.datetimeformat = 3 #We need to deal with date format else we get errors.
#Must find out why!
if execSQL( cmd, c):
# Check for result rows, some commands don't return results
# We can do this by checking rowcount of the connection object
# eg c.rowcount
if c.rowcount > 0: #Only create Frame for resultsets
headers = []
for col in c.description: #Get number of columns, rows and
headers.append(col[0]) #column titles (attribute names)
fID = wxNewId() #We will remember this
w = wxFrame(parent, fID, cmd, wxDefaultPosition, wxSize(200,100))
if wxPlatform == '__WXMSW__':
w.icon = wxIcon(cwd + 'bitmaps/new24x24.ico', wxBITMAP_TYPE_ICO)
w.SetIcon(w.icon)
sb = w.CreateStatusBar(2) # create a statusbar that shows
sb.SetStatusWidths([-1, 150]) # the time and date on the right
sb.SetStatusText("Rows = " + str(c.rowcount),1)
if n:
sb.SetStatusText(n,0)
tID = wxNewId()
t = wxListCtrl (w, tID, wxDefaultPosition, wxSize(100,100), wxLC_REPORT )
i = 0
nCols = len(headers)
for colHdr in headers: #Populate the list control header row
t.InsertColumn(i, "%s" % colHdr)
t.SetColumnWidth(i, wxLIST_AUTOSIZE_USEHEADER )
i += 1
w.Show(true)
row = 0
while 1: #Populate the remainder of the list control
rec = c.fetchone()
if not rec: break
z = ""
column = 0
for x in range(len(rec)):
z = "%s" % (rec[x])
if column == 0:
t.InsertStringItem(row, z)
elif column in range(1, nCols) :
if z: t.SetStringItem( row, column, z)
column += 1
row += 1
for i in range(nCols):
t.SetColumnWidth(i, wxLIST_AUTOSIZE_USEHEADER )
return w, fID #return window and its id. The id can be used by the parent
#if it wants to close the frame
else:
return None, None
else:
return None, None
I haven't shown a printing example but there are many different ways of doing the
printing, as there are many different ways of displaying on screen. One very simple
way of printing smart looking reports is to create an HTML or XML file and write
the result set to that. You need to know how to use HTML or XML, specifically how
to create tables. Then you can launch a browser from your app and click the print
button. You could also use the ReportLab tools which are very good:
cheers,
David