import random
import string
import pyodbc
import datetime
# 建立数据库连接
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=LAPTOP-78AGJMK2;DATABASE=garbage sorting;Trusted_Connection=yes;Encrypt=False;Connection Timeout=30;')
cursor = cnxn.cursor()
# 随机生成中文字符串
def generate_chinese(length):
return ''.join(random.sample(''.join(
[chr(i) for i in range(0x4e00, 0x9fa6)]), length))
# 随机生成联系电话
def generate_phone():
return ''.join(random.choices(string.digits, k=11))
# 随机生成日期
def generate_date():
start_date = datetime.date(2020, 1, 1)
end_date = datetime.date(2021, 12, 31)
random_days = random.randint(0, (end_date - start_date).days)
return start_date + datetime.timedelta(days=random_days)
# 随机生成街道长度
def generate_street_length():
return round(random.uniform(0.5, 10), 2)
# 随机生成车道数
def generate_lane_count():
return random.randint(1, 4)
# 随机生成房产号码
def generate_house_number():
return random.randint(1, 100)
# 随机生成垃圾箱尺寸
def generate_trash_size():
return round(random.uniform(0.5, 2), 2)
# 随机生成垃圾箱提供个数
def generate_trash_count():
return random.randint(1, 10)
# 随机生成垃圾箱提供日期
def generate_trash_date():
return generate_date()
# 随机生成垃圾箱提供RFID
def generate_trash_rfid():
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))
# 随机生成垃圾收集司机姓名
def generate_driver_name():
return ''.join(random.choices(string.ascii_uppercase, k=3)) + ' ' + ''.join(random.choices(string.ascii_uppercase, k=4))
# 随机生成垃圾收集司机电话
def generate_driver_phone():
return generate_phone()
# 随机生成垃圾种类
def generate_trash_type():
trash_types = ['可回收物', '有害垃圾', '厨余垃圾', '其他垃圾']
return random.choice(trash_types)
# 随机生成垃圾重量
def generate_trash_weight():
return round(random.uniform(0.1, 2), 2)
# 随机生成30个政府记录并插入到数据库中
for i in range(30):
government_name = generate_chinese(5) + '政府'
contact_person = generate_chinese(2)
contact_phone = generate_phone()
government_area = str(random.randint(100, 1000)) + '平方公里'
cursor.execute("INSERT INTO 政府 (政府名称, 联系人, 联系电话, 政府面积) VALUES (?, ?, ?, ?)",
government_name, contact_person, contact_phone, government_area)
# 随机生成30个街道记录并插入到数据库中
for i in range(30):
street_id = 'ST' + str(random.randint(1000000, 9999999))
government_name = generate_chinese(5) + '政府'
street_number = random.randint(1, 100)
street_name = generate_chinese(4) + '街道'
street_length = generate_street_length()
road_surface = random.choice(['水泥路面', '沥青路面'])
lane_count = generate_lane_count()
cursor.execute("INSERT INTO 街道 (ID, 政府名称, 街道ID, 街道名称, 街道长度, 道路表面类型, 车道数) VALUES (?, ?, ?, ?, ?, ?, ?)",
street_id, government_name, street_number, street_name, street_length, road_surface, lane_count)
# 随机生成30个房产记录并插入到数据库中
for i in range(30):
house_id = 'HS' + str(random.randint(1000000, 9999999))
government_name = generate_chinese(5) + '政府'
street_id = 'ST' + str(random.randint(1000000, 9999999))
house_number = generate_house_number()
cursor.execute("INSERT INTO 房产 (房产ID, 政府名称, ID, 街道号码) VALUES (?, ?, ?, ?)",
house_id, government_name, street_id, house_number)
# 随机生成30个房产所有者记录并插入到数据库中
for i in range(30):
owner_id = 'OWNER' + str(random.randint(100000, 999999))
owner_name = generate_chinese(2)
owner_phone = generate_phone()
cursor.execute("INSERT INTO 房产所有者 (房产者ID, 房产电话号码, 房产者姓名) VALUES (?, ?, ?)",
owner_id, owner_phone, owner_name)
# 随机生成30个房产所有者所属记录并插入到数据库中
for i in range(30):
house_id = 'HS' + str(random.randint(1000000, 9999999))
owner_id = 'OWNER' + str(random.randint(100000, 999999))
belong_id = 'BELONG' + str(random.randint(100000, 999999))
cursor.execute("INSERT INTO 房产所有者所属 (房产ID, 房产者ID, 所属ID) VALUES (?, ?, ?)",
house_id, owner_id, belong_id)
# 随机生成4种垃圾箱记录并插入到数据库中
trash_types = [('01', '可回收物'), ('02', '有害垃圾'), ('03', '厨余垃圾'), ('04', '其他垃圾')]
for trash_type in trash_types:
for i in range(10):
trash_size = generate_trash_size()
supply_cost = round(random.uniform(50, 100), 2)
cursor.execute("INSERT INTO 垃圾箱 (垃圾类型ID, 类型名称, 尺寸, 供应成本) VALUES (?, ?, ?, ?)",
trash_type[0], trash_type[1], trash_size, supply_cost)
# 随机生成30个垃圾箱提供记录并插入到数据库中
for i in range(30):
house_id = 'HS' + str(random.randint(1000000, 9999999))
trash_type = random.choice(trash_types)
trash_size = generate_trash_size()
trash_count = generate_trash_count()
supply_date = generate_trash_date()
rfid = generate_trash_rfid()
replace_count = random.randint(0, 2)
replace_reason = random.choice(['损坏', '丢失'])
cursor.execute("INSERT INTO 垃圾箱提供 (房产ID, 垃圾类型ID, 尺寸, 提供个数, 提供日期, RFID, 替换个数, 替换原因) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
house_id, trash_type[0], trash_size, trash_count, supply_date, rfid, replace_count, replace_reason)
# 随机生成10个垃圾收集司机记录并插入到数据库中
for i in range(10):
driver_id = 'DRIVER' + str(random.randint(1000000, 9999999))
driver_name = generate_driver_name()
driver_phone = generate_driver_phone()
driver_address = generate_chinese(10)
cursor.execute("INSERT INTO 司机 (司机ID, 司机姓名, 电话号码, 家庭住址) VALUES (?, ?, ?, ?)",
driver_id, driver_name, driver_phone, driver_address)
# 随机生成30个垃圾收集
cnxn.commit()
print(2)
cursor.close()
cnxn.close()
Traceback (most recent call last):
File "D:\mypython\sql\insert.py", line 7, in <module>
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=LAPTOP-78AGJMK2;DATABASE=garbage sorting;Trusted_Connection=yes;Encrypt=False;Connection Timeout=30;')
pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC SQL Server Driver]登录超时已过期 (0) (SQLDriverConnect); [HYT00] [Microsoft][ODBC SQL Server Driver]无效的连接字符串属性 (0)')
>>>
用这段代码连接到服务器,报着样的错,请问如何解决