首先,需要使用Python的csv模塊來讀取CSV文件中的數據。然后,使用MySQLdb模塊來連接到MySQL數據庫,并使用SQL語句來搜索數據庫中的數據。
示例代碼:
import csv
import MySQLdb
# Connect to the database
db = MySQLdb.connect(host="localhost", user="username", passwd="password", db="database")
# Create a cursor object
cursor = db.cursor()
# Open the CSV file
with open('data.csv', 'r') as csv_file:
# Read the CSV file
csv_reader = csv.reader(csv_file)
# Iterate over each row in the CSV file
for row in csv_reader:
# Execute the SQL query
cursor.execute("SELECT * FROM table WHERE column1 = %s AND column2 = %s", (row[0], row[1]))
# Fetch the result
result = cursor.fetchone()
# Print the result
print(result)
# Close the cursor
cursor.close()
# Close the connection
db.close()