当前有个需求,需要根据已知字段值批量查询数据库
1、先用py处理Excel构建出所有的sql
2、用下面的脚本批量执行构建出的sql
3、将执行结果输出到文本
# -*- coding: utf-8 -*-
import MySQLdb
db = MySQLdb.connect('127.0.0.1', 'root', '137@Apple', port=3306, db='bmsdb')
cur = db.cursor()
with open('aa1.txt', 'r') as f:
for line in f:
cur.execute(line)
results = cur.fetchall()
print(results)
cur.close()
db.close()
执行命令:
python search.py > cc.txt
可能的错误
Traceback (most recent call last):
File "search.py", line 2, in <module>
import MySQLdb
ModuleNotFoundError: No module named 'MySQLdb'
解决办法:
pip3 install pymysql mysqlclient