[python]代码库
import os
import time
import pymysql
print("""
目前系统所有数据库: manage_system shop_web_databases
""")
while True:
database = input("请输入需要访问的数据库:")
try:
mysql_link = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='123123',database=database,charset='utf8')
except:
print("数据库链接异常【可能是因为该数据库不存在或,line:11-->mysql_link代码的配置中出现错误所导致】,20sec之后系统中断...")
time.sleep(20)
exit()
crs = mysql_link.cursor()
if database == 'manage_system':
print("""
=====================
欢迎进入省份下属查询系统!
=====================
""")
if database == 'manage_system':
while True:
list = []
province = input("请输入查询省份:")
goods_line = crs.execute(f"select * from china as china1 inner join china as china2 on (china1.id=china2.pid) having (china1.name='{province}')")
for goods_data in crs.fetchmany(goods_line):
list.append(goods_data[4])
print(province+"下属地区:")
print(list)
write_y_s = input("是否需要保存数据至本地(y/n)? ")
if (write_y_s == 'y') or (write_y_s == 'Y'):
with open("province_data.txt",f"wb") as f:
f.write(province.encode("utf-8"))
f.write(str(list).encode("utf-8"))
print("保存成功!")
time.sleep(2)
else:
pass
input("Enter键继续...")
os.system("cls")
else:
print("抱歉,您未拥有该数据库的访问权限!")
[代码运行效果截图]
中级程序员
by: FN 发表于:2023-08-16 14:43:56 顶(1) | 踩(1) 回复
需要配合areas的SQL数据表使用,areas.sql网上到处可下载
回复评论