python: mysql数据库连接

mysql数据库连接

import pymysql
#1. 先连接到数据库
dblink = pymysql.Connect (host = '47.98.173.29',
    user = 'root',
    passwd = '123456',
    db = 'practice',
    port = 3306,
    charset = 'utf8')
#3. 获取游标
get_cursor = dblink.cursor()
#4. 执行sql
order = 'select stuname, stubirth from tbstudent;'
get_cursor.execute(order)
#5. 获取结果
data = get_cursor.fetchall()
#6.获取一个结果
# data = cursor.fetchone()
for i in data:
    print(i)
#2.关闭连接
dblink.close()

猜你喜欢

转载自blog.csdn.net/Darkman_EX/article/details/81040518