Web用例中,访问mysql数据库,并执行sql语句
<p>以下示例为获取数据库中,验证码短信中的验证码。</p><p>print("入参内容:", phnumber)</p><p>import pymysql </p><p><br>connection = pymysql.connect(<br> host='202.103.144.107',<br> user='wt',<br> password='WT$mysql2020',<br> database='osstest',<br> port=13306,<br> charset='utf8mb4',<br> #cursorclass=pymysql.cursors.DictCursor<br> cursorclass=pymysql.cursors.Cursor<br>)<br> <br>try:<br> with connection.cursor() as cursor:<br>sql = "SELECT SUBSTRING(strMessage,LOCATE('验证码:', strMessage)+4,6) AS strMessage FROM osstest.tb_sms_record where phones = %s order by createdate desc LIMIT 1"<br> cursor.execute(sql, (phnumber,))<br> result = cursor.fetchone()<br> print("数据库存储的验证码为:", result)<br> sys_return(result)<br><br>finally:<br> connection.close() # 确保连接关闭</p>
收藏(0)
分享
相关标签:
注意:本文归作者所有,未经作者允许,不得转载