使用Python获取Mysql数据

wuxqing的头像 wuxqing 1 2016-03-13 08:35 0

 基本信息

× 1    × 1   

浏览数: 6806

分享时间: 1 年 前

3
#!/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])




  • tumubai的头像 tumubai 2016-05-18 22:26 代码数:0

    谢谢楼主分享

  • cheonghu的头像 cheonghu 2017-01-21 09:14 代码数:0
    #连接数据库
    import pymysql;
    
    
    
    # 建立mysql链接
    def connectdb():
    
         try:
            conn = pymysql.connect(host='192.168.57.30', port=3306, user='root', passwd='root',
                                   db='test', charset='utf8')
         except:
                print("connect db error")
         finally:
             return conn
    
    
    
    def getAllDataByTemplatId():  
    try:  
    conn = db.connectdb() 
     cur = conn.cursor() 
     sql = "SELECT * FROM test uvr"  cur.execute(sql) 
     return cur.fetchall() 
     except:  print("some")  
    finally:  print("close db")

    最后编辑 2 个月 前

您的评论: