這篇文章給大家介紹如何在python中遠程連接MySQL數(shù)據(jù)庫,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
連接數(shù)據(jù)庫
這里默認大家都已經配置安裝好 MySQL 和 Python 的MySQL 模塊,且默認大家的DB內表和訪問賬號權限均已設置無誤,下面直接代碼演示:
import MySQLdb import cPickle as pk def write2file(DB_data,save_filename): """數(shù)據(jù)寫入本地""" with open(save_filename, 'w') as f: pk.dump(DB_data, f)) # 創(chuàng)建數(shù)據(jù)庫連接 conn2db = MySQLdb.connect( host='111.111.111.111',# host port = 3306, # 默認端口,根據(jù)實際修改 user='username',# 用戶名 passwd='passwd', # 密碼 db ='DBname', # DB name ) cur = conn2db.cursor() # 操作游標 DB_data = cur.execute("select * from table_name;") # SQL語句 ,查詢需要到處內容 # 獲取多條數(shù)據(jù) DB_datas = cur.fetchmany(DB_data) # 寫入本地 write2file(DB_datas,'save_table_name') """ # 打印表中數(shù)據(jù),chek data for info in DB_datas: print info """ cur.close() conn2db.commit() try: conn2db.close() # 關閉連接 print "closed connection..." except Exception,e: print Exception,":",e
關于如何在python中遠程連接MySQL數(shù)據(jù)庫就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)站題目:如何在python中遠程連接MySQL數(shù)據(jù)庫-創(chuàng)新互聯(lián)
文章源于:http://chinadenli.net/article34/eopse.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、域名注冊、定制網(wǎng)站、搜索引擎優(yōu)化、企業(yè)建站、外貿網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容