暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

用ChatGPT开发一个MySQL表信息查询系统

702

这一节内容,我们来尝试让ChatGPT帮我们开发一个MySQL的表状态查询系统。

首先问ChatGPT:

借助gradio实现一个页面,有三个下拉框,一个框是选择MySQL的IP,一个框选择MySQL的状态,一个框是填写库名,状态包括每张业务表的数据量、表大小、碎片率、主键自增值。状态获取均通过SQL语句实现。一个输出框,输出下拉框选择查看的内容结果。用一个Python文件实现


返回的代码如下(小编只修改了用户名、密码和host):

    import gradio as gr
    import mysql.connector


    # 连接MySQL数据库
    def connect_to_mysql(ip, db_name):
    try:
    cnx = mysql.connector.connect(user='martin', password='uqatcdaT12',
    host=ip, database=db_name)
    return cnx
    except Exception as e:
    print("Unable to connect to MySQL database: ", str(e))
    return None


    # 获取业务表状态信息
    def get_table_status(ip, db_name, status):
    cnx = connect_to_mysql(ip, db_name)
    if not cnx:
    return "Failed to connect to MySQL database."


    cursor = cnx.cursor()
    query = ""
    if status == "Table data count":
    query = "SELECT TABLE_NAME, TABLE_ROWS FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s"
    elif status == "Table size":
    query = "SELECT TABLE_NAME, (DATA_LENGTH + INDEX_LENGTH) AS `Size` FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s"
    elif status == "Fragmentation rate":
    query = "SELECT table_name, CONCAT(ROUND((data_free/(data_length+index_length))*100), '%') AS `Fragmentation Rate` FROM information_schema.tables WHERE table_schema = %s"
    elif status == "Primary key auto increment value":
    query = "SELECT TABLE_NAME, AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND AUTO_INCREMENT IS NOT NULL"

    cursor.execute(query, (db_name,))
    result = cursor.fetchall()
    if not result:
    return "No tables found in the database."

    table_info = ""
    if status == "Table data count":
    for row in result:
    table_name = row[0]
    table_rows = row[1]
    table_info += f"{table_name}: Rows - {table_rows}\n"
    elif status == "Table size":
    for row in result:
    table_name = row[0]
    table_size = row[1]
    table_info += f"{table_name}: Size - {table_size} bytes\n"
    elif status == "Fragmentation rate":
    for row in result:
    table_name = row[0]
    frag_rate = row[1]
    table_info += f"{table_name}: Fragmentation rate - {frag_rate}\n"
    elif status == "Primary key auto increment value":
    for row in result:
    table_name = row[0]
    auto_inc = row[1]
    table_info += f"{table_name}: Primary key auto increment value - {auto_inc}\n"

    return table_info


    # 定义页面输入项
    ip_dropdown = gr.inputs.Dropdown(choices=["127.0.0.1", "your_mysql_ip_2", "your_mysql_ip_3"],
    label="Select MySQL IP address")
    db_name_input = gr.inputs.Textbox(label="Enter database name")
    status_dropdown = gr.inputs.Dropdown(choices=["Table data count", "Table size", "Fragmentation rate", "Primary key auto increment value"],
    label="Select the status to view")


    # 定义页面输出项
    output_text = gr.outputs.Textbox(label="MySQL status information")


    # 定义应用程序
    def mysql_status_app(ip, db_name, status):
    output = get_table_status(ip, db_name, status)
    return output


    # 启动应用程序
    app_title = "MySQL Status Viewer"
    app_description = "View different status of your MySQL database tables."
    examples = [["your_mysql_ip_1", "your_database_name", "Table data count"]]
    gr.Interface(fn=mysql_status_app, inputs=[ip_dropdown, db_name_input, status_dropdown],
    outputs=output_text, title=app_title, description=app_description,
    examples=examples).launch()



    运行之后,登录http://127.0.0.1:7860/


    比如我们想知道martin库所有表的数据量


    数据量、表大小、碎片率、主键自增值都试了,可以满足我们的需求。

    根据这个思路,可以扩展更多的功能,比如库大小、当前活跃连接、参数查询等。

    最后介绍一下我们的AI星球:

    星球目前已经有30多篇高质量的内容,其中包括分析使用AI工具能做MySQL的哪些事情,比如我们创建了专栏《ChatGPT和MySQL结合》,专栏还在持续更新中。

    当然星球还包括其他AI内容,比如AI画图、个人或者企业怎么基于ChatGPT的API开发应用。以及围绕AI工具有哪些副业或者创业的思路。

    新老成员,进入星球三天后添加客服微信:PMThoughts_XP或者yzlkf09,可赠送ChatGPT账号和5美元额度的API KEY。

    犹豫的朋友们也可以先进星球看一下,看我们写的内容是否适合你,如果觉得不满意,星球支持三天内全额退款的,你也不会有任何损失。

    再送一张28元的优惠券,券后60元年费(相当于每天0.16元),限量50张,先到先得。

    文章转载自MySQL数据库联盟,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

    评论