代码自动生成工具实战-Cursor
创始人
2025-05-29 15:00:06

文章目录

    • Cursor code generate介绍
    • Cursor 代码生成实战
      • prompt1
      • prompt2
      • prompt3
      • prompt4
    • 我的感受

Cursor code generate介绍

之前看过github copilot 的代码生成能力。可以说解放了码农的双手,基础的代码完全可以来生成。可是后来它收费了。

今天在网上发现这个免费的code generate。
下面官网
在这里插入图片描述

Cursor 代码生成实战

prompt1

prompts : 比较两个数据库的差异,数据库的ip地址是10.50.10.170,…

在这里插入图片描述

prompt2

我的prompts: 是两个数据库比较差异,第二个数据库是的ip是10.50.10.171. 两个数据库的port都是5432

# 导入所需库
import psycopg2# 连接数据库
connection1 = psycopg2.connect(host='10.50.10.170', port=5432, user='username', password='xxx', dbname='qmstst')
cursor1 = connection1.cursor()connection2 = psycopg2.connect(host='10.50.10.171', port=5432, user='username', password='xxx', dbname='qmstst')
cursor2 = connection2.cursor()# 从文件中获取表名
with open("tables.txt", "r") as file:table_names = [line.strip() for line in file.readlines()]differences = []for i in range(len(table_names) - 1):# 查询两个表的数据量cursor1.execute(f"SELECT COUNT(*) FROM {table_names[i]}")table1_count = cursor1.fetchone()[0]cursor2.execute(f"SELECT COUNT(*) FROM {table_names[i+1]}")table2_count = cursor2.fetchone()[0]# 计算数据量差异difference = abs(table1_count - table2_count)differences.append(difference)# 输出结果到文件
with open("output.txt", "w") as output_file:for i in range(len(differences)):output_file.write(f"表 {table_names[i]} 和表 {table_names[i+1]} 的数据量差异为: {differences[i]}\n")# 关闭数据库连接
cursor1.close()
connection1.close()cursor2.close()
connection2.close()

prompt3

promtps : 最终输出结果是有差异的表,结果需要有时间。需要包含哪个ip的哪个表。

在这里插入图片描述

# 导入所需库
import psycopg2
from datetime import datetime# 连接数据库
connection1 = psycopg2.connect(host='10.50.10.170', port=5432, user='username', password='xxx', dbname='qmstst')
cursor1 = connection1.cursor()connection2 = psycopg2.connect(host='10.50.10.171', port=5432, user='username', password='xxx', dbname='qmstst')
cursor2 = connection2.cursor()# 从文件中获取表名
with open("tables.txt", "r") as file:table_names = [line.strip() for line in file.readlines()]differences = []for i in range(len(table_names) - 1):# 查询两个表的数据量cursor1.execute(f"SELECT COUNT(*) FROM {table_names[i]}")table1_count = cursor1.fetchone()[0]cursor2.execute(f"SELECT COUNT(*) FROM {table_names[i+1]}")table2_count = cursor2.fetchone()[0]# 计算数据量差异difference = abs(table1_count - table2_count)if difference > 0:differences.append((table_names[i], table_names[i+1], difference))# 输出结果
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
for table1, table2, difference in differences:message = f"{current_time} - IP: 10.50.10.170 表 {table1} 和 IP: 10.50.10.171 表 {table2} 的数据量差异为: {difference}"print(message)connection1.close()
cursor2.close()
connection2.close()

prompt4

将输出结果推送到企业微信。

在这里插入图片描述

from datetime import datetime# 连接数据库
connection1 = psycopg2.connect(host='10.50.10.170', port=5432, user='username', password='xxx', dbname='qmstst')
cursor1 = connection1.cursor()connection2 = psycopg2.connect(host='10.50.10.171', port=5432, user='username', password='xxx', dbname='qmstst')
cursor2 = connection2.cursor()# 从文件中获取表名
with open("tables.txt", "r") as file:table_names = [line.strip() for line in file.readlines()]differences = []for i in range(len(table_names) - 1):# 查询两个表的数据量cursor1.execute(f"SELECT COUNT(*) FROM {table_names[i]}")table1_count = cursor1.fetchone()[0]cursor2.execute(f"SELECT COUNT(*) FROM {table_names[i+1]}")table2_count = cursor2.fetchone()[0]# 计算数据量差异difference = abs(table1_count - table2_count)if difference > 0:differences.append((table_names[i], table_names[i+1], difference))# 导入企业微信库
from wxwork_bot import WxWorkBot# 初始化企业微信机器人
bot = WxWorkBot("your_bot_key")# 输出结果并发送到企业微信报警
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
for table1, table2, difference in differences:message = f"{current_time} - IP: 10.50.10.170 表 {table1} 和 IP: 10.50.10.171 表 {table2} 的数据量差异为: {difference}"bot.send_text(message)
connection1.close()cursor2.close()
connection2.close()

我的感受

  • 从prompt1可以看出代码生成之后还会提示合并还是丢弃,这个比较人性化。因为不确定cursor对prompts的理解,这样的好处是可以及时调整prompts来生成更加符合要求的结果
  • 从prompt3来看这个还是会一行一行扫描,而不是直接定位到需要修改的地方。这一点比较慢。
  • 其实可以只对部分代码进行框选进行优化,这个速度应该会提升。
  • 竟然可以回滚prompt。

相关内容

热门资讯

荼蘼什么意思 岁月缱绻葳蕤生香... 感谢作者【辰夕】的原创独家授权分享编辑整理:【多肉植物百科】百科君坐标:云南 曲靖春而至,季节流转,...
cad打印线条粗细设置 cad... 004-线型(下)打印样式设置和线型文件使用一、线宽设置方法制图规范里边的线宽要求,我们已经定义好,...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...