Solved

Going Crazy Need Help Python Formatting sqlite out put

Posted on 2010-09-21
9
534 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 84 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 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 17

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Make the most of your online learning experience.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

617 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