您的位置:首页 > 编程语言 > Python开发

Python2 连接MySQL

2016-05-07 16:09 417 查看
先安装MySQL-python

yum install -y MySQL-python


pip install mysqlclient (Windows)

测试代码:

# -*- coding: utf-8 -*-
import os
import MySQLdb

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

conn=MySQLdb.connect(host="127.0.0.1",port=22066,user="root",passwd="123456",db="dsideal_db",charset="utf8")
cursor=conn.cursor()
sql = "SELECT file_name FROM t_resource_info WHERE res_type = 1 AND GROUP_ID = 1 AND RESOURCE_SIZE_INT >10000000 ORDER BY CREATE_TIME LIMIT 1000"
n = cursor.execute(sql)
for row in cursor.fetchall():
file_id = row[0]
file_id_2 = file_id[:2]
path = "/Material/"+file_id_2+"/"+file_id
if os.path.exists(path):
print "正在删除文件:"+path
os.remove(path)
else:
print "未找到文件:"+path
print "完成"
cursor.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: