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

python连接mysql

2016-10-20 21:34 316 查看
#!/usr/bin/python
# -*- coding: UTF-8 -*-

# pip install MySQL-python

import MySQLdb, os

try:
conn = MySQLdb.connect(host='172.17.42.1', user='数据库访问用户', passwd='数据库访问密码', db='数据库名', port=3306)

cur = conn.cursor()
cur.execute('SELECT `id`, `name`, `path`, FROM `doc_file`')

# 获取全部记录
results=cur.fetchall()
for r in results:
id, name, path = r[0], r[1], r[2]

if path and not os.path.exists(path):
print 'file not exist: ', id, name, path, flashpath

cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: