Please don't look at these unless you are really struggling!
#!/usr/bin/env python3 import pymysql.cursors # Set parameters dbname = "biodb" dbhost = "pandora" dbuser = "xxxx" # Ask a demonstrator dbpass = "xxxx" # Ask a demonstrator port = 3306 # Create SQL statement to find number of human protein entries sql = "select count(*) from protein where source = 'Homo sapiens'" # Connect to the database db = pymysql.connect(host=dbhost, port=port, user=dbuser, passwd=dbpass, db=dbname) # Create a cursor and execute the SQL on it cursor = db.cursor() nrows = cursor.execute(sql) # We know there is only one row returned to fetch it and print the data data = cursor.fetchone() print ("There were ", data[0], " rows")
#!/usr/bin/env python3 import pymysql.cursors # Set parameters dbname = "biodb" dbhost = "pandora" dbuser = "xxxx" # Ask a demonstrator dbpass = "xxxx" # Ask a demonstrator port = 3306 # Create SQL statement to find information for PDB entry 1DM5 sql = "select source, resolution, description from protein where pdb_code = '1DM5'" # Connect to the database db = pymysql.connect(host=dbhost, port=port, user=dbuser, passwd=dbpass, db=dbname) # Create a cursor and execute the SQL on it cursor = db.cursor() nrows = cursor.execute(sql) # We know there is only one row returned to fetch it and print the data data = cursor.fetchone() src = data[0] resol = data[1] desc = data[2] print ("PDB Code: 1DM5") print ("Species: ", src) print ("Resolution: ", resol) print ("Description: ", desc)
This version uses repeated calls to cursor.fetchone()
#!/usr/bin/env python3 import pymysql.cursors # Set parameters dbname = "biodb" dbhost = "pandora" dbuser = "xxxx" # Ask a demonstrator dbpass = "xxxx" # Ask a demonstrator port = 3306 # Create SQL statement to find information for proteins from Leishmania sql = "select pdb_code, resolution, name from protein where source = 'LEISHMANIA MAJOR'" # Connect to the database db = pymysql.connect(host=dbhost, port=port, user=dbuser, passwd=dbpass, db=dbname) # Create a cursor and execute the SQL on it cursor = db.cursor() nrows = cursor.execute(sql) # Use repeated calls to cursor.fetchone() data = cursor.fetchone() while data is not None: pdb = data[0] resol = data[1] name = data[2] print (pdb, " ", resol, " ", name) data = cursor.fetchone()
This version uses the preferred method if using the cursor as an iterator.
#!/usr/bin/env python3 import pymysql.cursors # Set parameters dbname = "biodb" dbhost = "pandora" dbuser = "xxxx" # Ask a demonstrator dbpass = "xxxx" # Ask a demonstrator port = 3306 # Create SQL statement to find information for proteins from Leishmania sql = "select pdb_code, resolution, name from protein where source = 'LEISHMANIA MAJOR'" # Connect to the database db = pymysql.connect(host=dbhost, port=port, user=dbuser, passwd=dbpass, db=dbname) # Create a cursor and execute the SQL on it cursor = db.cursor() nrows = cursor.execute(sql) # Using the cursor as an iterator for data in cursor: pdb = data[0] resol = data[1] name = data[2] print (pdb, " ", resol, " ", name)