and so it connects to the server and it is supposed to print out a first name
and last name of someone on the contact list. But right now i only know how to
access the most recently added contact. How can i access additional rows?
Thanks for any help I hope you understand!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ok I have the following code so far:
import MySQLdb
conn = MySQLdb.connect( host = "localhost",user =
"username",passwd = "myPw",db =
"myDB",unix_socket='/opt/lampp/var/mysql/mysql.sock')
cursor = conn.cursor ()
cursor.execute ("SELECT * FROM Contacts")
result = cursor.fetchone()
print "Hey,", result, result
cursor.close ()
conn.close ()
and so it connects to the server and it is supposed to print out a first name
and last name of someone on the contact list. But right now i only know how to
access the most recently added contact. How can i access additional rows?
Thanks for any help I hope you understand!
The default cursor can be used as an iterator:
for row in cursor:
print "Hey,", row, row
Or you can just call fetchone() repeatedly (in a loop) to fetch a row at a
time. It'll return None when the results are exhausted
while True:
row = cursor.fetchone()
if row is None:
break
print "Hey,", row, row
Or, you can use fetchall() to grab them all at once:
rows = cursor.fetchall()
for row in rows:
print "Hey,", row, row
THanks so much for the help it worked!