Solved

Going Crazy Need Help Python Formatting sqlite out put

Posted on 2010-09-21
9
527 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
9 Comments
 
LVL 9

Accepted Solution

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

Assisted Solution

by:gelonida
gelonida earned 166 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 16

Assisted Solution

by:gelonida
gelonida earned 166 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
 
LVL 28

Assisted Solution

by:pepr
pepr earned 250 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 28

Assisted Solution

by:pepr
pepr earned 250 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 28

Assisted Solution

by:pepr
pepr earned 250 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 28

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now