<div>I have used fetchall() to insert the values from a table into a tuple. anywhere from 0 - ? many rows could be in this tuple, so it is a row within a row. How do I use a loops to iterate through the nested tuple, and assign the tuples integers and strings to variables, ugh :P
</div>
<div> </div>
<div>The Tables data is like this: </div>
<div> </div>
<div>+---------------------------------------------------------------------------------------------------------------------+</div>
<div>| ID (INT) | Protocol (TEXT) | Name (LONGTEXT) Description (LONGTEXT) | </div>
<div>+---------------------------------------------------------------------------------------------------------------------+</div>
<div> </div>
<div> </div>
<div><font size="2">
<p> </p>
<p>import MySQLdb</p>
<p>import sys</p>
<p> </p>
<p># find out what this ports function is ----</p>
<p>def grabPortInfo(port):</p>
<p>try:</p>
<p>conn=MySQLdb.connect( host="<a href="http://www.freesql.org">www.freesql.org</a>",</p>
<p>user="portnumbers",</p>
<p>port=3306,</p>
<p>passwd="*********",</p>
<p>db="portnumbers")</p>
<p>except MySQLdb.Error, e: </p>
<p>print "Error %d: %s" % (e.args[0], e.args[1]) </p>
<p>print "\n\n WTF!? - portnumbers MySQL DB didn't open"</p>
<p>sys.exit (1)</p>
<p># ------------------</p>
<p>cursor = conn.cursor()</p>
<p>stmt = "SELECT * FROM portnumbers WHERE port=%i" %port</p>
<p>cursor.execute(stmt)</p>
<p>numrows = int(cursor.rowcount)</p>
<p>if numrows == 0:</p>
<p>print "port # not found in database"</p>
<p>elif numrows >= 1:</p>
<p>result = cursor.fetchall()</p>
<p>print len(result)</p>
<p># break tuple into strings with output </p>
<p>myPort = port</p>
<p>myProtocol = result[0|3]</p>
<p>myName = result[0|4]</p>
<p>myDescription = result[0|5]</p>
<p>print "PORT # | PROTOCOL | PORT NAME | PORT DESCRIPTION "</p>
<p>print myPort, myProtocol, myName, myDescription</p>
<p>cursor.close()</p>
<p>conn.close()</p>
<p># end function -------------</p>
<p> </p>
<p>grabPortInfo(22)</p>
<p> </p>
<p> </p></font></div>