使用Python獲取Mysql數據

wuxqing 8年前發布 | 49K 次閱讀 Python MySQL

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

</pre>

 本文由用戶 wuxqing 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!