当前位置: 首页 > article >正文

OpenAI 实战进阶教程 - 第七节: 与数据库集成 - 生成 SQL 查询与优化

内容目标
  • 学习如何使用 OpenAI 辅助生成和优化多表 SQL 查询
  • 了解如何获取数据库结构信息并与 OpenAI 结合使用

实操步骤

1. 创建 SQLite 数据库示例

创建数据库及表结构:

import sqlite3

# 连接 SQLite 数据库(如果不存在则创建)
conn = sqlite3.connect("company_data.db")
cursor = conn.cursor()

# 创建 employees 表
cursor.execute('''
CREATE TABLE IF NOT EXISTS employees (
    id INTEGER PRIMARY KEY,
    name TEXT,
    department_id INTEGER,
    salary REAL,
    hire_date TEXT
)
''')

# 创建 departments 表
cursor.execute('''
CREATE TABLE IF NOT EXISTS departments (
    id INTEGER PRIMARY KEY,
    name TEXT,
    budget REAL
)
''')

# 插入示例数据
cursor.executemany('''
INSERT OR IGNORE INTO employees (id, name, department_id, salary, hire_date)
VALUES (?, ?, ?, ?, ?)
''', [
    (1, "Alice", 1, 8500, "2022-03-15"),
    (2, "Bob", 2, 6200, "2023-05-01"),
    (3, "Charlie", 1, 9300, "2021-11-12"),
])

cursor.executemany('''
INSERT OR IGNORE INTO departments (id, name, budget)
VALUES (?, ?, ?)
''', [
    (1, "Engineering", 500000),
    (2, "HR", 150000)
])

conn.commit()
conn.close()
print("Database setup complete.")

2. 自动读取数据库结构信息

使用 PRAGMA table_info() 查询元信息,以便将表结构传递给 OpenAI:

def get_table_info(db_name):
    conn = sqlite3.connect(db_name)
    cursor = conn.cursor()

    # 获取所有表名
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
    tables = cursor.fetchall()
    
    table_info = {}
    for table_name in tables:
        table_name = table_name[0]
        cursor.execute(f"PRAGMA table_info({table_name});")
        columns = cursor.fetchall()
        table_info[table_name] = [column[1] for column in columns]

    conn.close()
    return table_info

db_name = "company_data.db"
table_structure = get_table_info(db_name)
print("Database Structure:", table_structure)

3. 生成两表关联查询

将数据库结构作为上下文传入 OpenAI,请求生成 SQL 查询:

import openai

# 设置 API 密钥
openai.api_key = "your-api-key"

# 构建提示信息
table_info_prompt = f"""
The database has the following structure:
Table `employees`: id, name, department_id, salary, hire_date
Table `departments`: id, name, budget
Write an SQL query to find the names of employees in the 'Engineering' department whose salary exceeds 8000.
The query should join the employees and departments tables.
"""

# 调用 OpenAI 生成 SQL 查询
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": table_info_prompt}],
    max_tokens=150
)

sql_query = response['choices'][0]['message']['content']
print("Generated SQL Query:")
print(sql_query)

4. 示例生成结果
SELECT e.name
FROM employees e
JOIN departments d ON e.department_id = d.id
WHERE d.name = 'Engineering' AND e.salary > 8000;

小结

  • 元信息读取:通过 PRAGMA table_info() 获取数据库表结构
  • 查询生成:将表名、字段及业务规则传递给 OpenAI,可以生成跨表关联查询
  • 应用场景:适用于复杂业务查询,如员工信息与部门预算的联动分析

练习题

  1. 实践查询生成
    修改查询条件,让 OpenAI 生成一个查询语句,找出预算大于 300,000 且部门中员工平均工资超过 7000 的部门名称。

  2. 优化查询
    使用 OpenAI 请求优化生成的 SQL 查询,确保执行效率更高。


http://www.kler.cn/a/531102.html

相关文章:

  • 【C语言入门】解锁核心关键字的终极奥秘与实战应用(二)
  • css三角图标
  • ECharts 样式设置
  • Java手写简单Merkle树
  • 使用 HTTP::Server::Simple 实现轻量级 HTTP 服务器
  • 【2025年最新版】Java JDK安装、环境配置教程 (图文非常详细)
  • 98.2 AI量化开发:基于DeepSeek打造个人专属金融消息面-AI量化分析师(理论+全套Python代码)
  • I.MX6ULL 中断介绍下
  • 【linux学习指南】线程概念与控制
  • 【大数据技术】教程03:本机PyCharm远程连接虚拟机Python
  • 玩转ChatGPT:DeepSeek测评(科研思路梳理)
  • Codeforces Round 981 (Div. 3)
  • 【模块化编程关键字】C语言模块化编程关键技术及其应用研究
  • 机试题——到邻国目标城市的最短距离
  • 基于单片机的智能感控杆设计(论文+源码)
  • 【电路笔记】-计数器与分频
  • Tree Compass( Codeforces Round 934 (Div. 2) )
  • 如何生成强密码:提高网络安全性的全面指南
  • 【C语言入门】解锁核心关键字的终极奥秘与实战应用(三)
  • win32汇编环境,窗口程序中使用进度条控件
  • 上海路网道路 水系铁路绿色住宅地工业用地面图层shp格式arcgis无偏移坐标2023年
  • HarmonyOS:给您的应用添加通知
  • 计算机网络 应用层 笔记 (电子邮件系统,SMTP,POP3,MIME,IMAP,万维网,HTTP,html)
  • 【Linux】--- 基础IO
  • 用deepseek R1把本地的AI工具都做成离线
  • !力扣 84. 柱状图中最大矩形