zoukankan      html  css  js  c++  java
  • Python

    步骤:

    1.导包 =>2.建立链接 =>3.创建游标 =>4.获取结果 =>5.关闭资源

    这里需要注意第五步是必不可少的,我们打开资源就需要关闭资源,避免造成内存浪费

    # 1.导包
    import pymysql
    
    # 2.建立mysql通道
    db = pymysql.connect(host='127.0.0.1', user='root', password='12345678', database='cx_study')
    # 这里的sql为您需要执行的数据库命令语句
    sql = 'select * from cxtable'
    # 3.创建游标
    cursor = db.cursor()
    # execute()方法执行sql语句
    cursor.execute(sql)
    # 4.获取结果
    # fetchone()获取一行结果(首行)
    res = cursor.fetchall()
    print(res)
    # 5.关闭资源
    db.close()
    # 遍历结果
    for i in res:
        print(i[0], i[1], i[2], i[3], i[4])

    拿到结果过后我们就可以进行其他操作了

  • 相关阅读:
    2019/1/2
    2018/12/22
    2018/12/18
    2018/12/17
    2018/12/16
    编程总结汇总
    学习总结汇总
    第十三周学习总结--助教
    第十一周编程总结
    第10周编程总结
  • 原文地址:https://www.cnblogs.com/cxstudypython/p/12128322.html
Copyright © 2011-2022 走看看