首页 > 数据库技术 > 详细

python接口自动化测试二十六:使用pymysql模块链接数据库

时间:2018-06-09 17:29:09      阅读:216      评论:0      收藏:0      [点我收藏+]
 #!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/5/28 18:51
# @Author  : StalloneYang
# @File    : mysql_技术分享图片test.py
# @desc:


# 连接数据库

import pymysql.cursors


# 连接MySQL数据库
connection = pymysql.connect(host=‘localhost‘, port=3306, user=‘yang‘, password=‘‘, db=‘test_yang‘, charset=‘utf8mb4‘, cursorclass=pymysql.cursors.DictCursor)

# 通过cursor创建游标
cursor = connection.cursor()

# 执行数据查询
sql = "SELECT `id`, `name`,‘address‘ FROM `tb_emp` WHERE NAME = ‘杨大大‘"
cursor.execute(sql)

#查询数据库单条数据
result = cursor.fetchone()
print(result)
print(type(result))

print("-----------华丽分割线------------")

# 执行数据查询
sql = "SELECT `id`, `name`,‘address‘ FROM `tb_emp`"
cursor.execute(sql)

#查询数据库多条数据
result = cursor.fetchall()
for data in result:
    print(data)

# 关闭数据连接
connection.close() 
技术分享图片

 

python接口自动化测试二十六:使用pymysql模块链接数据库

原文:https://www.cnblogs.com/zhongyehai/p/9160288.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!