PyMysql

import pymysql

#  连接数据库
con = pymysql.connect(
    host = 'localhost',
    user = 'root',
    password = '',
    db = 'python',
    charset = 'utf8'
)

try:
    # 获取会话指针
    with con.cursor() as cursor:
        sql = "select * from code"
        # 得到总记录数
        count = cursor.execute(sql)
        print(count)
        # 获得所有数据
        res = cursor.fetchall()
        for i in res:
            print(i)

finally:
    con.close()

你可能感兴趣的:(PyMysql)