You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.9 KiB

5 months ago
import os
import platform
5 months ago
from Text2Sql.Util.Text2SqlUtil import *
from Text2Sql.Util.PostgreSQLUtil import PostgreSQLUtil
from Text2Sql.Util.SaveToExcel import save_to_excel
5 months ago
if __name__ == "__main__":
vn = DeepSeekVanna()
5 months ago
# 开始训练
print("开始训练...")
5 months ago
# 打开CreateTable.sql文件内容
5 months ago
with open("Sql/CreateTable.sql", "r", encoding="utf-8") as file:
5 months ago
ddl = file.read()
# 训练数据
vn.train(
ddl=ddl
)
5 months ago
# 自然语言提问
5 months ago
question = '''
查询每个区每个校都上传了多少课程数量需要返回行政区名称学校名称上传课程数量等属性.
先按行政区划排序再按课程数量由高到低排序'''
# ,只要行政区划是二道区的
# question = '''
# 查询小学语文每个章节下资源的数量,除了章节名称和资源数量外,
# 还需要返回所属册的名称,
# 按册进行排序,第二级排序使用章节号,
# 输出顺序为:册名称,章节名称,资源数量,字段名称也按上面的中文来描述'''
5 months ago
# 开始查询
print("开始查询...")
5 months ago
# 获取完整 SQL
sql = vn.generate_sql(question)
5 months ago
print("生成的查询 SQL:\n", sql)
5 months ago
# 执行SQL查询
with PostgreSQLUtil() as db:
sample_data = db.execute_query(sql)
5 months ago
filename = "d:/导出信息.xlsx"
5 months ago
save_to_excel(sample_data, filename)
# 在代码最后添加自动打开逻辑
if platform.system() == "Windows":
try:
full_path = os.path.abspath(filename)
print(f"\n✅ 文件已保存到:{full_path}")
os.startfile(full_path) # 关键代码
except Exception as e:
print(f"\n⚠️ 自动打开失败: {str(e)},请手动打开文件")
else:
print("\n⚠️ 非Windows系统请手动打开文件")