使用pymysql连接数据库

import pymysql
conn = pymysql.connect(
    host="10.00.0.00",
    port=31379,
    user="root",
    password="123456",
    database="acc_test"
)
 # 模拟从数据库获取单个字段值
with conn.cursor() as cursor:
    # 获取【不良率】
    sql1 = "SELECT trans_amount  FROM  acc_cus.cus_repay_trans_flow WHERE project_no = 'BUWS10043190001' AND flow_sn = '1296240919574950115'"
    cursor.execute(sql1)
    result1 = cursor.fetchall()
    defective_rate = str(result1[0][0])
    defective_rate_num = int(result1[0][0])
    # 获取【年利率】
    sql2 = "SELECT trans_amount  FROM  acc_cus.cus_repay_trans_flow WHERE project_no = 'BUWS10043190001' AND flow_sn = '1296240919574950116'"
    cursor.execute(sql2)
    result2 = cursor.fetchall()
    year_rate = str(result2[0][0])
    year_rate_num = int(result2[0][0])
	
# 关闭数据库连接    
conn.close()