#1
  1. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13

    Question Seperating a column's value out of a row of columns


    Code:
            try: 
                    cur.execute( '''SELECT URL, hits FROM counters ORDER BY hits DESC''' ) 
            except MySQLdb.Error, e: 
                    print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) 
            else: 
                    data = cur.fetchall() 
                     
                    for row in data: 
                            print ( "<tr>" ) 
                                     
                            for item in row: 
                                    print( '''<td>  <a href='http://www.%s?show=log'>%s</a>  </td>''' % (item, item) ) 
                             
                    sys.exit(0)
    1. ruteruns a dataset
    2. seperate each rows
    3. itermate over the items of a row.

    Okey, so far BUT i want the url linking to happen only for the URL column's value, and not for the hits column too.
    How do i apply the url link to the URL column's value only?
    What is now proved was once only imagined!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Code:
            try: 
                    cur.execute( '''SELECT URL, hits FROM counters ORDER BY hits DESC''' ) 
            except MySQLdb.Error, e: 
                    print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) 
            else: 
                    data = cur.fetchall() 
                    for row in data:
                            (URL,hits,) = row
                            print ( "<tr>" )
                            print( '''<td>  <a href='http://www.%s?show=log'>%s</a>  </td>''' % (URL, item) )
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    Thanks can you helpe with this too?

    Code:
            try: 
                    cur.execute( '''SELECT host, userOS, browser, hits, lastvisit FROM visitors 
                                                    WHERE counterID = (SELECT ID FROM counters WHERE URL = %s) ORDER BY lastvisit DESC''', (htmlpage,) ) 
            except MySQLdb.Error, e: 
                    print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) 
            else:                         
                    data = cur.fetchall() 
                             
                    for host, useros, browser, hits, lastvisit in data: 
                            print ( "<tr>" ) 
    
                            for item in host, useros, browser, hits, lastvisit.strftime('%A %e %b, %H:%M').decode('cp1253').encode('utf8'): 
                                    print ( "<td><center><b><font color=white> %s </td>" % item ) 
                             
                    sys.exit(0)
    That would be also written as:

    Code:
    for row in data: 
        print ("tr>") 
        for item in row: 
            print( "blah blah blah" )

    And that would make the code easier to read and more clear, but its that 'lastvisit' column's value than needs formating,hence it makes me use the above syntax.

    Is there any simpler way to write the above working code without the need to specify all of the columns' names into the loop?
    What is now proved was once only imagined!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Code:
            try:
                    cur.execute( '''SELECT host, userOS, browser, hits, lastvisit FROM visitors
                                                    WHERE counterID = (SELECT ID FROM counters WHERE URL = %s) ORDER BY lastvisit DESC''', (htmlpage,) )
            except MySQLdb.Error, e:
                    print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
            else:
                    data = cur.fetchall()
    
                    for row in data:
                            row = list(row)
                            row[-1] = row[-1].strftime('%A %e %b, %H:%M').decode('cp1253').encode('utf8')
                            print ( "<tr>" )
                            for item in row:
                                    print ( "<td><center><b><font color=white> %s </td>" % item )

    Comments on this post

    • Nik agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    Thessaloniki
    Posts
    1,285
    Rep Power
    13
    thank you very much!
    What is now proved was once only imagined!

IMN logo majestic logo threadwatch logo seochat tools logo