#!/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()
原文地址:https://www.cnblogs.com/zhongyehai/p/9160288.html