环境:windows10+Python3.6

  1. 安装Python

    下载路径: 选择自己的版本

  2. 直接安装,然后配置环境变量

3.安装pymysql

 这里我是直接用pip安装的,命令:

  pip install pymysql

4.连接数据库

  

import pymysql

# 打开数据库连接

db = pymysql.connect("localhost","develop","5tgb^YHN","test" )

# 使用 cursor() 方法创建一个游标对象 cursor

cursor = db.cursor()

sql = "SELECT * from gds.pub_exchange_calendar"

# 使用 execute()  方法执行 SQL 查询 

cursor.execute(sql)

# 使用 fetchone() 方法获取单条数据.

# 使用fetchall() 方法取得结果集

data = cursor.fetchall()

for row in data:

id = row[0]

date = row[1]

print ("id is  : %d " % id,"date is %s" %date)

# 关闭数据库连接

db.close()

结果报错,如下

后面查了下原因,是因为我的文件名叫select.py  文件名是关键字,所以改了文件名就好了

5.成功运行