Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Going Crazy Need Help Python Formatting sqlite out put

Posted on 2010-09-21
9
Medium Priority
?
535 Views
Last Modified: 2012-05-10
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
Comment
Question by:dolamitejenkins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 9

Accepted Solution

by:
zaghaghi earned 336 total points
ID: 33736829
I think python doc is a good help. http://docs.python.org/library/sqlite3.html
0
 
LVL 17

Assisted Solution

by:gelonida
gelonida earned 664 total points
ID: 33737936
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
 
LVL 17

Assisted Solution

by:gelonida
gelonida earned 664 total points
ID: 33737977
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Assisted Solution

by:pepr
pepr earned 1000 total points
ID: 33738520
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
 
LVL 29

Assisted Solution

by:pepr
pepr earned 1000 total points
ID: 33738526
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
 
LVL 29

Assisted Solution

by:pepr
pepr earned 1000 total points
ID: 33738540
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
 
LVL 29

Expert Comment

by:pepr
ID: 33738563
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
 

Author Closing Comment

by:dolamitejenkins
ID: 33738601
solved
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question