玩蛇网提供最新Python编程技术信息以及Python资源下载!

python遍历数据库表及其相关表操作

python 培训

python遍历数据库表及其相关表操作.

相关文章推荐:
Python 并行遍历zip()函数使用方法
Python遍历文件夹目录与文件操作

#!/usr/bin/python

import MySQLdb
import getopt
import sys
from datetime import datetime

conf_host = '127.0.0.1'
conf_port = 3306
conf_user = 'root'
conf_pass = ''
conf_db = 'information_schema'
conf_bind_db = 'mydns'

def main():

    db_bind = MySQLdb.connect(host = conf_host, port = conf_port, user = conf_user, passwd = conf_pass, db = conf_bind_db)
    cursor_bind = db_bind.cursor()
    db = MySQLdb.connect(host = conf_host, port = conf_port, user = conf_user, passwd = conf_pass, db = conf_db)
    cursor = db.cursor()
    file = open("output.txt","w")
    cursor.execute("select table_name from tables where table_name regexp'rg_' and table_schema ='" + conf_bind_db + "';")
    #www.iplaypython.com

    for row in cursor.fetchall():
        print row[0]
        sql = "select zone,host,type,data,ttl from  " + row[0] + " where zone regexp'(cnc|ct|edu|ov).centos.com' and data='originboss.centos.com.' and type='cname';"
        cursor_bind.execute(sql)
        result = cursor_bind.fetchall()
        for row in result:
	            print row[0],row[1],row[2],row[3],row[4]        
    cursor.close()
    db.close()    
    
    cursor_bind.close()
    db_bind.close()
    
if __name__ == '__main__':
    main()

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/base/b2471.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-04-07 15:04 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部