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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sqlalchemy import create_engine, text
|
| 4 |
+
import pymysql
|
| 5 |
|
| 6 |
+
# 数据库连接配置
|
| 7 |
+
DB_CONFIG = {
|
| 8 |
+
'host': 'rm-j6c5yhe0l739e7752vo.mysql.cnhk.rds.aliyuncs.com',
|
| 9 |
+
'user': 'report_user',
|
| 10 |
+
'password': 'report_user_123',
|
| 11 |
+
'database': 'easy_financial_report'
|
| 12 |
+
}
|
| 13 |
|
| 14 |
+
def get_database_url():
|
| 15 |
+
"""构造数据库连接URL"""
|
| 16 |
+
return f"mysql+pymysql://{DB_CONFIG['user']}:{DB_CONFIG['password']}@{DB_CONFIG['host']}/{DB_CONFIG['database']}"
|
| 17 |
+
|
| 18 |
+
def connect_to_database():
|
| 19 |
+
"""创建数据库连接引擎"""
|
| 20 |
+
try:
|
| 21 |
+
engine = create_engine(get_database_url())
|
| 22 |
+
return engine
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"数据库连接失败: {e}")
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
def execute_query(query):
|
| 28 |
+
"""执行SQL查询并返回结果"""
|
| 29 |
+
if not query.strip():
|
| 30 |
+
return "请输入SQL查询语句"
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# 创建数据库连接引擎
|
| 34 |
+
engine = create_engine(get_database_url())
|
| 35 |
+
|
| 36 |
+
# 执行查询并返回DataFrame
|
| 37 |
+
df = pd.read_sql_query(query, engine)
|
| 38 |
+
engine.dispose()
|
| 39 |
+
return df
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return f"查询执行失败: {str(e)}"
|
| 42 |
+
|
| 43 |
+
def get_table_names():
|
| 44 |
+
"""获取数据库中的所有表名"""
|
| 45 |
+
try:
|
| 46 |
+
# 创建数据库连接引擎
|
| 47 |
+
engine = create_engine(get_database_url())
|
| 48 |
+
|
| 49 |
+
# 查询所有表名
|
| 50 |
+
query = "SHOW TABLES"
|
| 51 |
+
df = pd.read_sql_query(query, engine)
|
| 52 |
+
engine.dispose()
|
| 53 |
+
|
| 54 |
+
# 返回表名列表
|
| 55 |
+
return df.iloc[:, 0].tolist() if not df.empty else []
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return [f"获取表名失败: {str(e)}"]
|
| 58 |
+
|
| 59 |
+
def preview_table(table_name):
|
| 60 |
+
"""预览表的前几行数据"""
|
| 61 |
+
if not table_name or "失败" in table_name:
|
| 62 |
+
return "请选择有效的表名"
|
| 63 |
+
|
| 64 |
+
query = f"SELECT * FROM {table_name} LIMIT 10"
|
| 65 |
+
return execute_query(query)
|
| 66 |
+
|
| 67 |
+
# 新增功能函数
|
| 68 |
+
def insert_record(title):
|
| 69 |
+
"""向report_file_link表插入新记录"""
|
| 70 |
+
if not title.strip():
|
| 71 |
+
return "请输入标题"
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
engine = create_engine(get_database_url())
|
| 75 |
+
# 插入新记录
|
| 76 |
+
query = "INSERT INTO report_file_link (title) VALUES (:title)"
|
| 77 |
+
with engine.connect() as conn:
|
| 78 |
+
conn.execute(text(query), {"title": title})
|
| 79 |
+
conn.commit()
|
| 80 |
+
engine.dispose()
|
| 81 |
+
return f"成功插入记录: {title}"
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return f"插入记录失败: {str(e)}"
|
| 84 |
+
|
| 85 |
+
def update_record(record_id, new_title):
|
| 86 |
+
"""更新report_file_link表中的记录"""
|
| 87 |
+
if not record_id or not new_title.strip():
|
| 88 |
+
return "请输入记录ID和新标题"
|
| 89 |
+
|
| 90 |
+
try:
|
| 91 |
+
engine = create_engine(get_database_url())
|
| 92 |
+
# 更新记录
|
| 93 |
+
query = "UPDATE report_file_link SET title = :title WHERE id = :id"
|
| 94 |
+
with engine.connect() as conn:
|
| 95 |
+
result = conn.execute(text(query), {"title": new_title, "id": record_id})
|
| 96 |
+
conn.commit()
|
| 97 |
+
engine.dispose()
|
| 98 |
+
|
| 99 |
+
if result.rowcount > 0:
|
| 100 |
+
return f"成功更新记录ID {record_id} 的标题为: {new_title}"
|
| 101 |
+
else:
|
| 102 |
+
return f"未找到ID为 {record_id} 的记录"
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return f"更新记录失败: {str(e)}"
|
| 105 |
+
|
| 106 |
+
def delete_record(record_id):
|
| 107 |
+
"""从report_file_link表中删除记录"""
|
| 108 |
+
if not record_id:
|
| 109 |
+
return "请输入记录ID"
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
engine = create_engine(get_database_url())
|
| 113 |
+
# 删除记录
|
| 114 |
+
query = "DELETE FROM report_file_link WHERE id = :id"
|
| 115 |
+
with engine.connect() as conn:
|
| 116 |
+
result = conn.execute(text(query), {"id": record_id})
|
| 117 |
+
conn.commit()
|
| 118 |
+
engine.dispose()
|
| 119 |
+
|
| 120 |
+
if result.rowcount > 0:
|
| 121 |
+
return f"成功删除ID为 {record_id} 的记录"
|
| 122 |
+
else:
|
| 123 |
+
return f"未找到ID为 {record_id} 的记录"
|
| 124 |
+
except Exception as e:
|
| 125 |
+
return f"删除记录失败: {str(e)}"
|
| 126 |
+
|
| 127 |
+
def refresh_report_file_link():
|
| 128 |
+
"""刷新report_file_link表的数据"""
|
| 129 |
+
return execute_query("SELECT * FROM report_file_link")
|
| 130 |
+
|
| 131 |
+
# 创建Gradio界面
|
| 132 |
+
with gr.Blocks(title="MySQL数据库查询工具") as demo:
|
| 133 |
+
gr.Markdown("# MySQL数据库查询工具")
|
| 134 |
+
gr.Markdown("连接到阿里云RDS MySQL数据库")
|
| 135 |
+
|
| 136 |
+
with gr.Tab("SQL查询"):
|
| 137 |
+
with gr.Row():
|
| 138 |
+
with gr.Column():
|
| 139 |
+
query_input = gr.Textbox(
|
| 140 |
+
label="SQL查询语句",
|
| 141 |
+
placeholder="请输入SQL查询语句,例如:SELECT * FROM table_name LIMIT 10",
|
| 142 |
+
lines=5
|
| 143 |
+
)
|
| 144 |
+
query_btn = gr.Button("执行查询")
|
| 145 |
+
with gr.Column():
|
| 146 |
+
query_output = gr.Dataframe(label="查询结果")
|
| 147 |
+
|
| 148 |
+
query_btn.click(
|
| 149 |
+
fn=execute_query,
|
| 150 |
+
inputs=query_input,
|
| 151 |
+
outputs=query_output
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
with gr.Tab("表浏览"):
|
| 155 |
+
with gr.Row():
|
| 156 |
+
with gr.Column():
|
| 157 |
+
table_dropdown = gr.Dropdown(
|
| 158 |
+
choices=get_table_names(),
|
| 159 |
+
label="选择表"
|
| 160 |
+
)
|
| 161 |
+
refresh_btn = gr.Button("刷新表列表")
|
| 162 |
+
preview_btn = gr.Button("预览表数据")
|
| 163 |
+
with gr.Column():
|
| 164 |
+
table_output = gr.Dataframe(label="表数据预览")
|
| 165 |
+
|
| 166 |
+
refresh_btn.click(
|
| 167 |
+
fn=get_table_names,
|
| 168 |
+
inputs=[],
|
| 169 |
+
outputs=table_dropdown
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
preview_btn.click(
|
| 173 |
+
fn=preview_table,
|
| 174 |
+
inputs=table_dropdown,
|
| 175 |
+
outputs=table_output
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
with gr.Tab("report_file_link管理"):
|
| 179 |
+
gr.Markdown("## report_file_link表管理")
|
| 180 |
+
|
| 181 |
+
with gr.Row():
|
| 182 |
+
with gr.Column():
|
| 183 |
+
# 插入功能
|
| 184 |
+
gr.Markdown("### 插入新记录")
|
| 185 |
+
insert_title = gr.Textbox(label="标题")
|
| 186 |
+
insert_btn = gr.Button("插入记录")
|
| 187 |
+
insert_result = gr.Textbox(label="插入结果")
|
| 188 |
+
|
| 189 |
+
# 更新功能
|
| 190 |
+
gr.Markdown("### 更新记录")
|
| 191 |
+
update_id = gr.Textbox(label="记录ID")
|
| 192 |
+
update_title = gr.Textbox(label="新标题")
|
| 193 |
+
update_btn = gr.Button("更新记录")
|
| 194 |
+
update_result = gr.Textbox(label="更新结果")
|
| 195 |
+
|
| 196 |
+
# 删除功能
|
| 197 |
+
gr.Markdown("### 删除记录")
|
| 198 |
+
delete_id = gr.Textbox(label="记录ID")
|
| 199 |
+
delete_btn = gr.Button("删除记录")
|
| 200 |
+
delete_result = gr.Textbox(label="删除结果")
|
| 201 |
+
|
| 202 |
+
with gr.Column():
|
| 203 |
+
# 显示当前数据
|
| 204 |
+
gr.Markdown("### 当前数据")
|
| 205 |
+
refresh_data_btn = gr.Button("刷新数据")
|
| 206 |
+
data_output = gr.Dataframe(label="report_file_link表数据")
|
| 207 |
+
|
| 208 |
+
# 设置按钮点击事件
|
| 209 |
+
insert_btn.click(
|
| 210 |
+
fn=insert_record,
|
| 211 |
+
inputs=insert_title,
|
| 212 |
+
outputs=insert_result
|
| 213 |
+
)
|
| 214 |
+
|
| 215 |
+
update_btn.click(
|
| 216 |
+
fn=update_record,
|
| 217 |
+
inputs=[update_id, update_title],
|
| 218 |
+
outputs=update_result
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
delete_btn.click(
|
| 222 |
+
fn=delete_record,
|
| 223 |
+
inputs=delete_id,
|
| 224 |
+
outputs=delete_result
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
refresh_data_btn.click(
|
| 228 |
+
fn=refresh_report_file_link,
|
| 229 |
+
inputs=[],
|
| 230 |
+
outputs=data_output
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
# 页面加载时自动显示数据
|
| 234 |
+
demo.load(
|
| 235 |
+
fn=refresh_report_file_link,
|
| 236 |
+
inputs=[],
|
| 237 |
+
outputs=data_output
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
if __name__ == "__main__":
|
| 241 |
+
# demo.launch(server_name="0.0.0.0", server_port=7861)
|
| 242 |
+
demo.launch(ssr_mode=False, share=True)
|