python连接mysql数据库

# 安装
pip install pymysql

# 连接数据库
connect = pymysql.connect(
    host='ip',
    port=port,
    user='root',
    passwd='password',
    db='db_name',
    charset='utf8')
    
# 创建游标对象
cursor = connect.cursor()

# sql
sql_data = 'sql语句'

# 提交
cursor.execute(sql_data)

# 返回单个元组, 如果没有则返回 null
result_data = cursor.fetchone()

# 返回多个元组, 如果没有则返回 ()
result_data = cursor.fetchall()

# 关闭连接
cursor.close()

 

posted @ 2020-07-07 17:39  Echo丶Mikasa  阅读(137)  评论(0)    收藏  举报