from pymysql import connect

def main():
    # 创建connection连接
    conn = connect(host='localhost', port=3306, user='root', password='123456',database = 'scott', charset = 'utf8')

    # 获得Cursor对象
    cs1 = conn.cursor()

    # 执行sql语句,并返回受影响的行数
    count = cs1.execute('select id,name from goods where id>=4')

    # 打印受影响的行数
    print('查寻到%d条数据' % count)

    for i in range(count):
        # 获取查询的结果
        result = cs1.fetchone()  # 逐条获取 fetchmany(3) 一次获取3条,fetchall() 获取所有
        # 打印查询的结果
        print(result)
        # 获取查询的结果

        # 关闭Cursor对象
        cs1.close()
        conn.close()


if __name__ == '__main__':
    main()

 

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐