You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

89 lines
2.2 KiB

import pymysql
import xlrd
"""
一、连接mysql数据库
"""
# 打开数据库连接
conn = pymysql.connect(
host='8.135.8.221', # MySQL服务器地址
user='root', # 用户名
password='qniaothreetwoonego', # 密码
charset='utf8', #
port=3308, # 端口
db='dating-agency-service', # 数据库名称
)
# 使用cursor()方法获取操作游标
c = conn.cursor()
# """
# 二、读取excel文件
# """
# FilePath = 'D:/LYL/Study/Robot_framework/space/Dating-Agency/相亲用户资料.xls'
#
# # 1.打开excel文件
# wkb = xlrd.open_workbook(FilePath)
# # 2.获取sheet
# sheet = wkb.sheet_by_index(0) # 获取第一个sheet表['用户资料']
# # 3.获取总行数
# rows_number = sheet.nrows
# # 4.遍历sheet表中所有行的数据,并保存至一个空列表cap[]
# cap = []
# for i in range(1,rows_number):
# x = sheet.row_values(i) # 获取第i行的值(从0开始算起)
# cap.append(x-1)
# print(cap)
# """
# 三、将读取到的数据批量插入数据库
# """
# for Userinfo in cap:
# # U = int(Userinfo[0])
# Uname = Userinfo[0]
# Usex = Userinfo[1]
# Uhight = int(Userinfo[2])
# Uwight = int(Userinfo[3])
# Uage = int(Userinfo[4])
# Ucity = Userinfo[5]
# Uedu = Userinfo[6]
# Umarry = Userinfo[7]
# Uincome = Userinfo[8]
# Unickname = Userinfo[9]
# # 使用f-string格式化字符串,对sql进行赋值
# c.execute(f"insert into student(Sno,Sname,Ssex,Sage,Sdept) value ('{Uname}','{Usex}','{Uhight}','{Uage}','{Ucity}','{Uedu}','{Umarry}','{Uincome}','{Unickname}')")
# conn.commit()
# conn.close()
# print("插入数据完成!")
"""
读取phone-text.txt文件的第一列数据
"""
phone_list = []
try:
file = open('D:/LYL/Test/ZTB/phone - test.txt', 'r')
except FileNotFoundError:
print('File is not found')
else:
lines = file.readlines()
for line in lines:
a = line.split(',')
x = a[0]
phone_list.append(x)
file.close()
#
for Phone in phone_list:
print(Phone)
"""
三、查询数据库account表中tid=Phone的所有数据
"""
for Phone in phone_list:
print(Phone)
c.execute(f"SELECT * FROM `uec`.`qn_account` WHERE `tid` = '{Phone}' AND `is_delete` = '0'")
conn.commit()
conn.close()