• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

Going Crazy Need Help Python Formatting sqlite out put

I need help and I know the answer is simple but I very new and cant figure it out

Im using sqlite3 and python2.6


I devlopped a database that gathers information


Code: Select all
con = lite.connect('semsdata.sqlite')
            cur = con.cursor()
            job_id=self.job_id
            Last_Name = self.tc1.GetValue()
            First_Name = self.tc2.GetValue()
                         
            cur.execute('insert into ptrecords values(?,?,?)',
                        (job_id, Last_Name, First_Name))
            con.commit()
            cur.close()
            con.close()
and it works perfectly

im trying to get the data back out and then in a for

so I can assign it to a variable as such




Code: Select all
def GetData(self):
        con = lite.connect('semsdata.sqlite')
           
           
        cur = con.cursor()
           
         
        cur.execute("select * from ptrecords where job_id = '%s'" % item.GetText())
                       
        rows = cur.fetchall()
        print rows
        print rows[:2]  

and it retrieves the data from the rows but I want to print the data to widgets as such




Code: Select all
self.data2 = wx.TextCtrl(panel, -1,job_id,pos=(200,50), size=(150, -1))
        self.data3 = wx.TextCtrl(panel, -1,First_Name,pos=(400,50), size=(150, -1))
        self.data4 = wx.TextCtrl(panel, -1,Last_Name,pos=(10,100), size=(150, -1))
how do I get the data from sqlite3 and into my widget ? Any help would be greatly appreciated Thanksdolamite
Python Fan

 
Posts: 6
Joined: Sat Sep 18, 2010 4:37 pm
Private messageTop
--------------------------------------------------------------------------------

0
dolamitejenkins
Asked:
dolamitejenkins
6 Solutions
 
Hamed ZaghaghiProgrammerCommented:
I think python doc is a good help. http://docs.python.org/library/sqlite3.html
0
 
gelonidaCommented:
I never used wxwidgets, but normally most text or label widgets shoud have a function to set the text contained in it.

rows_to_display = len(rows)
# now you know how many rows you have to display

for row in rows: # iterate over rows
    jobid, lastnam, firstname = row # exttract fields

0
 
gelonidaCommented:
aoplogies, EE doesn't handle pressin gthe tab key :-(

so I accidentlly posted my inclomplete message.

Below a small code snippet, which shows the frame to populate widgets
as I don't know wx all wx specific code is missing.

rows_to_display = len(rows)
# now you know how many rows you have to display

for row in rows: # iterate over rows
    jobid, lastnam, firstname = row # exttract fields
    # fetch and modify your widgets here
    # or create a new widget with the correct contents

      
By the way:
wit QT or perhaps some other gui frame works you would implement a data model
and a view model and connect both.

this is more comon for displaying data base queries.


again: I don't know whether wx supports Data Vie models
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
peprCommented:
If I understand it well, you get the list of tuples. In simple case, you want to convert it into a multiline string and put that string into the TexCtrl like:

self.data2.ChangeValue(s)

The following snippet simulates the rows and shows how to convert it into a multiline string.


rows = [ ('row1', 1, 3.56),    # one row = one tuple
         ('row2', 2, 5.6),
         ('row3', 3, None),
       ]
    
lst = []         # init       
for t in rows:   # for each tuple
    r = []       # for the tuple converted to the list of strings
    for e in t:
        r.append(str(e))
        
    s = ', '.join(r)    # join the strings from one row into one string
    lst.append(s)       # and append it to the resulting list of strings

result = '\n'.join(lst) # join the strings into a multiline strings
print result

Open in new window

0
 
peprCommented:
However, it can be written more briefly when using so called "list comprehension"
rows = [ ('row1', 1, 3.56),    # one row = one tuple
         ('row2', 2, 5.6),
         ('row3', 3, None),
       ]
    
lst = []         # init       
for t in rows:   # for each tuple
    r = [ str(e) for e in t ]  # the same as above but with the "list comprehension"
    s = ', '.join(r)    # join the strings from one row into one string
    lst.append(s)       # and append it to the resulting list of strings

result = '\n'.join(lst) # join the strings into a multiline strings
print result

Open in new window

0
 
peprCommented:
The lines 8 and 9 can be put together using the .join method of the separator string directly for the generator expression that was earlier the part of the list comprehension.
rows = [ ('row1', 1, 3.56),    # one row = one tuple
         ('row2', 2, 5.6),
         ('row3', 3, None),
       ]
    
lst = []         # init       
for t in rows:   # for each tuple
    s = ', '.join(str(e) for e in t)  # directly with the generator expression
    lst.append(s)       # and append it to the resulting list of strings

result = '\n'.join(lst) # join the strings into a multiline strings
print result

Open in new window

0
 
peprCommented:
Similarly, the lines 6 to 9 can be converted into another generator expression that can be directly substituted into the line 11.  Then you get one-liner (the line 6).
rows = [ ('row1', 1, 3.56),    # one row = one tuple
         ('row2', 2, 5.6),
         ('row3', 3, None),
       ]
    
result = '\n'.join(', '.join(str(e) for e in t) for t in rows) # one-liner
print result

Open in new window

0
 
dolamitejenkinsAuthor Commented:
solved
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now