zoukankan      html  css  js  c++  java
  • python查询MySQL数据库的表以及所有字段

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import pymysql
    
    # 查询所有字段
    def list_col(localhost, username, password, database, tabls_name):
        db = pymysql.connect(localhost, username, password, database, charset="utf8")
        cursor = db.cursor()
        cursor.execute("select * from %s" % tabls_name)
        col_name_list = [tuple[0] for tuple in cursor.description]
        db.close()
        return col_name_list
    
    # 列出所有的表
    def list_table(localhost, username, password, database):
        db = pymysql.connect(localhost, username, password, database, charset="utf8")
        cursor = db.cursor()
        cursor.execute("show tables")
        table_list = [tuple[0] for tuple in cursor.fetchall()]
        db.close()
        return table_list
    
    
    username = "root" # 用户名
    password = "root" # 连接密码
    localhost = "localhost" # 连接地址
    database = "school" # 数据库名
    tables = list_table(localhost, username, password, database) # 获取所有表,返回的是一个可迭代对象
    print(tables) 
    
    for table in tables:
        col_names = list_col(localhost, username, password, database, table)
        print(col_names) # 输出所有字段名

    来源:Python:查询 mysql 的所有表和字段 -> pymysql 演示

    版权声明:文章为CSDN博主「显魄-Simple」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_33811662/article/details/80855430

  • 相关阅读:
    UVA 120 Stacks of Flapjacks
    HDU 4869 Turn the pokers
    HDU 4882 ZCC Loves Codefires
    HDU 4864 Task
    HDU 4861 Couple doubi
    UVA 1600 Patrol Robot
    UVA 712 S-Trees
    2014/4/6长沙多校第六次(浙大校赛)
    UVA10905 思维考察
    HDU1498 枚举+二分图类棋盘问题(最大匹配)
  • 原文地址:https://www.cnblogs.com/Qi77/p/12496835.html
Copyright © 2011-2022 走看看