python连接mysql

import pymysql
# 两种python连接mysql
'''
# 第一种
con = pymysql.connect(
    host = '127.0.0.1',
    port = 9999,
    user='tj_msq',
    password='123456',
    db='test',
    charset='utf8'
)

# 测试连接
cursor = con.cursor()
cursor.execute('select 1')         # 连接成功会打印1
re = cursor.fetchone()
print(re)
# 关闭连接
cursor.close()
con.close()
'''

'''
# 第二种
config = {
    'host' : '127.0.0.1',
    'port' : 9999,
    'user' : 'tj_msq',
    'password' : '123456',
    'db' : 'test',
    'charset' : 'utf8'
}

con = pymysql.connect(**config)
# 测试连接
cursor = con.cursor()
cursor.execute('select 1')
re = cursor.fetchone()
print(re)
# 关闭连接
cursor.close()
con.close()
'''
原文地址:https://www.cnblogs.com/tangpg/p/8176209.html