Solved

Going Crazy Need Help Python Formatting sqlite out put

Posted on 2010-09-21
9
531 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 29

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
 
LVL 29

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 29

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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

763 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