.Tang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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()
'''

 

posted on 2018-01-02 11:05  .Tang  阅读(193)  评论(0编辑  收藏  举报