简体   繁体   中英

Facing problem in matching query from database using python and fetchall()

I have two tables in my database, protein_complex representing a set of proteins在此处输入图像描述

And here is nonprotein_complex representing a set of non-proteins. 在此处输入图像描述

The proteins and non proteins are referred to by their codes.

The python program that I have written, aims to take a code from a user, it is supposed to search both the database tables and tell whether the belongs to a protein, non-protein or neither. Here's the code:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="root",
  database="abdb"
)


while True:
    ch = int(input("\n1.Search by code\n2.Search by species\n\nPlease choose an option "))
    if ch==1:
        log =input("Enter the code value for the respective protein\n")
        mycursor = mydb.cursor(buffered=True)
        mycursor.execute("SELECT * FROM protein_complex WHERE code_name='%s' ",log)
        mycursor2 = mydb.cursor(buffered=True)
        mycursor2.execute("SELECT * FROM nonprotein_complex WHERE name='%s' ",log)
        
        rows1=mycursor.fetchall()
        rows2=mycursor2.fetchall()
        if rows1 is None:
            if rows2 is None:
                print("Not present")
            else:
                print("Non protein")
        else:
            print("protein")
    
    

However, in the output, whenever I enter a code, it is only classified into a protein. It never goes to the 'non-protein' or 'none' part. Could you please help me out why this is happening?

Here's the output:

在此处输入图像描述

Because if and else statements don't work like that. If the first if statement is true, then the second one won't even execute. That means in your example if rows1 is None: is not the case then else: print("protein") will execute. In your case, the only thing you print out in the else statment is "protein" but nothing else. To actually print both, protein or not, you have to print "protein" and "non protein" in every statement (if you always want to show both results).

Replace your if else statements with the code below, then it should work:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="root",
  database="abdb"
)


while True:
    ch = int(input("\n1.Search by code\n2.Search by species\n\nPlease 
choose an option "))
    if ch==1:
        log =input("Enter the code value for the respective protein\n")
        mycursor = mydb.cursor(buffered=True)
        mycursor.execute("SELECT * FROM protein_complex",log)
        mycursor2 = mydb.cursor(buffered=True)
        mycursor2.execute("SELECT * FROM nonprotein_complex",log)

    for row in mycursor:
        if str(log) == str(row):
            print("Protein")
        else:
            print("Not present")
          
    for row in mycursor2:
        if str(log) == str(row):
            print("Nonprotein")
        else:
            print("Not present")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM