python实现数据库查询的简单示例
2016-05-11 17:49
841 查看
#-*-coding:utf-8-*- '"author:GoGoCaptain"' import sys import MySQLdb def Main(): f1=open('md5.txt','r')#打开需要查询的md5 f2=open('result.txt','w+')#查询结果写到result里 conn=MySQLdb.connect(host='xxxxx',user='xxxx',passwd='xxxx',db='xxxx',port=xxxx) cur=conn.cursor() i = 0 md5 = f1.readline() while md5 != '': sql = "SELECT file_md5 FROM `v_scanresult_all` where cert_md5='%s'"%md5.split('\n')[0]#数据库查询语句,可根据实际需求修改 cur.execute(sql)#执行数据库查询语句 result = cur.fetchall()#获取查询结果 data = str(result).split(',') for d in data: if len(d)>20: d = d.replace(',','').replace(')','').replace('(','').replace('\'','').replace(' ','') #删除无用字符,方便查看 f2.write(d + '\n') i += 1 print md5.split('\n')[0],'-->ok--count:' + str(i) #f2.write(str(result) + '\n') md5 = f1.readline() f1.close() f2.close() conn.close() if __name__ == '__main__': Main()
相关文章推荐
- python计算文件的md5值
- Python学习笔记-json解析
- python3 无覆盖写入&读入
- 用 Python 和 OpenCV 检测图片上的条形码Detecting Barcodes in Images with Python and OpenCV
- python爬虫学习笔记——使用requests库编写爬虫(1)
- Python logging模块详解
- python的json格式文件读写
- python文件处理
- 1.python基础学习__函数中的参数
- import CSV data into MySQL via PyMySQL in Python 3.3
- pyhon基础(一)
- python内建函数及属性
- leetcode(5),Valid Anagram(python)
- python 爬虫
- python 异步IO
- python 协程
- Python os.stat() 方法
- Python Web 开发的十个框架【转载】
- 用Python做自然语言处理必知的八个工具【转载】
- python encode和decode函数说明