之前看过github copilot 的代码生成能力。可以说解放了码农的双手,基础的代码完全可以来生成。可是后来它收费了。
今天在网上发现这个免费的code generate。
下面官网
prompts : 比较两个数据库的差异,数据库的ip地址是10.50.10.170,…
我的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()
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()
将输出结果推送到企业微信。
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()