设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2121|回复: 2

使用Python获取Mysql数据

  [复制链接]

171

主题

398

金钱

605

积分

初级用户

发表于 2016-7-15 16:59:58 | 显示全部楼层 |阅读模式
本帖最后由 chhy 于 2016-7-15 17:01 编辑

来源:http://www.open-open.com/code/view/1457829300325

#!/usr/bin/python# -*- coding: UTF-8 -*-# pip install MySQL-pythonimport 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])


python

python

2

主题

2031

金钱

2058

积分

中级用户

发表于 2016-7-26 15:16:32 | 显示全部楼层
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. # pip install MySQL-python
  4. import MySQLdb, os
  5. try:   
  6.   conn = MySQLdb.connect(host='172.17.42.1', user='数据库访问用户', passwd='数据库访问密码', db='数据库名', port=3306)   
  7.   cur = conn.cursor()   
  8.   cur.execute('SELECT `id`, `name`, `path`, FROM `doc_file`')   
  9. # 获取全部记录   
  10.   results=cur.fetchall()   
  11.   for r in results:        
  12.     id, name, path = r[0], r[1], r[2]        
  13.     if path and not os.path.exists(path):            
  14.     print 'file not exist: ', id, name, path, flashpath   
  15.     cur.close()   
  16.   conn.close()
  17. except MySQLdb.Error,e:     
  18.   print "Mysql Error %d: %s" % (e.args[0], e.args[1])
复制代码
回复 支持 1 反对 0

使用道具 举报

2

主题

2031

金钱

2058

积分

中级用户

发表于 2016-7-26 15:17:03 | 显示全部楼层

重新给楼主排版一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

快速回复 返回顶部 返回列表