python2 和python3 连接mysql

来源:互联网 发布:java需求分析主要干啥 编辑:程序博客网 时间:2024/05/25 16:37

Python2 连接MySQL

先安装MySQL-python

yum install -y MySQL-python
pip install MySQL-python

# -*- coding: utf-8 -*-import osimport MySQLdbimport sysreload(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 "未找到文件:"+pathprint "完成"cursor.close()

Python3 连接MySQL

sudo apt-get install python3-pymysql


py文件:


from pymysql import *


conn2 = connect(host='localhost', port=3306, user='root',password='mysql', database='DataBu',charset='utf8')


count = cs1.execute('CALL cur_detail(%s,%s)',params_detail)




指定向python2或python3中安装包:


sudo pip install -t /usr/local/lib/python2.7/site-packages/ xlwt


sudo pip install -t /usr/local/lib/python3.5/site-packages/ xlwt


在 ~/.profile 下一定要配置python2 和python3 的路径,这是指定当前用户的环境变量
export PYTHONPATH=/usr/bin/python2.7:/usr/bin/python3.5:$PYTHONPATH
修改后保存并source更新一下
source ~/.bashrc
这样python2和python3 会使用各自site-packages下面的包




原创粉丝点击