python操作mysql怎么使用变量进行查询
python操作mysql如何使用变量进行查询
有一个ip列表
格式如:
1.1.1.1
2.2.2.2
3.3.3.3
需要从数据库中查询对应的域名 并把域名进行去重
import MySQLdb
ip = open(r'C:\Users\Downloads\out.data').read()
items = ip.split('\n')
conn=MySQLdb.connect(host="localhost",user="root",passwd="password",db="db");
cur = conn.cursor()
cur.exceute="select domainname from dns where responsedata like '%ip%';"
想请教下 like里面那个ip如何使用ip列表里的ip啊
希望输出结果是
1.1.1.1 sina.com
2.2.2.2 taobao.com
3.3.3.3 qq.com
如果是没查到的话
则
4.4.4.4 no result
请大牛指点下
------解决方案--------------------
有一个ip列表
格式如:
1.1.1.1
2.2.2.2
3.3.3.3
需要从数据库中查询对应的域名 并把域名进行去重
import MySQLdb
ip = open(r'C:\Users\Downloads\out.data').read()
items = ip.split('\n')
conn=MySQLdb.connect(host="localhost",user="root",passwd="password",db="db");
cur = conn.cursor()
cur.exceute="select domainname from dns where responsedata like '%ip%';"
想请教下 like里面那个ip如何使用ip列表里的ip啊
希望输出结果是
1.1.1.1 sina.com
2.2.2.2 taobao.com
3.3.3.3 qq.com
如果是没查到的话
则
4.4.4.4 no result
请大牛指点下
------解决方案--------------------
- Python code
"select domainname from dns where responsedata like '%" + ip + "%';"
------解决方案--------------------
- Python code
import MySQLdb conn=MySQLdb.connect(host="localhost",user="root",passwd="password",db="db"); cur = conn.cursor() for ip in open(r'C:\Users\Downloads\out.data').read().split('\n'): cur.exceute("select domainname from dns where responsedata like '%%%s%%'"% ip) rs = cur.fetchone() print ip, 'No result' if not rs else rs[0]