PyMysql

2017-11-24  本文已影响0人  __construct
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()
上一篇下一篇

猜你喜欢

热点阅读