Python 3.x python中通过文件查询MariaDB

Python 3.x python中通过文件查询MariaDB,python-3.x,mariadb,Python 3.x,Mariadb,我有一个项目,我想通过文件逐行查询MariaDB。它都是用python编写的。我遇到的问题是,如果找到,它将只打印最后一行,而不是所有匹配项。我的期望是,每次找到匹配项时,它都会打印到终端。非常感谢您的帮助 import mysql.connector import time file1 = open("sample.unique.big", "r") count = 0 mydb = mysql.connector.connect( host=&qu

我有一个项目,我想通过文件逐行查询MariaDB。它都是用python编写的。我遇到的问题是,如果找到,它将只打印最后一行,而不是所有匹配项。我的期望是,每次找到匹配项时,它都会打印到终端。非常感谢您的帮助

import mysql.connector
import time
file1 = open("sample.unique.big", "r") 
count = 0

mydb = mysql.connector.connect(
host="10.0.0.72",
user="admin",
password="#########",
database="#########"
)
start = time.time()
while True: 
count += 1

# Get next line from file 
line = file1.readline() 

# if line is empty 
# end of file is reached 
if not line: 
    break
#print("{}".format(line.strip()))
#print("Line{}: {}".format(count, line.strip())) 
mycursor = mydb.cursor()
sql = "SELECT * FROM ether2 use index (idx_privadd) WHERE privadd = %s"
adr = (line, )
mycursor.execute(sql, adr)
myresult = mycursor.fetchall()
for x in myresult:
  print(x)
end = time.time()
print(end-start)

我通过首先格式化readline实现了我想要的结果。第二,myresults的if语句。完成的代码看起来像

import mysql.connector
import time
file1 = open("sample.unique.big", "r") 
count = 0

mydb = mysql.connector.connect(
host="10.0.0.72",
user="*****",
password="*****",
database="etherpro"
)
start = time.time()
while True: 
    count += 1
    # Get next line from file 
    line = file1.readline() 
    #Format the line
    search=("{}".format(line.strip()))
# if line is empty 
# end of file is reached 
    if not line: 
        break
    mycursor = mydb.cursor()
    sql = "SELECT id FROM ether2 WHERE privadd = %s"
    adr = (search, )
    mycursor.execute(sql, adr)
    myresults = mycursor.fetchone()
    if myresults:
        print("oh snap, the id is:",myresults)
end = time.time()
print(end-start)