PDBC详细介绍

1.PDBC
  • 利用第三方库pymysql
  • 内置库
  • 第三方库
    • 查看(cmd)
      • pip list
      • 工程  project interpreter查看
    • 安装
    • 卸载
      • pip Uninstall 库名
    • 安装路径
      • python目录——lib——site-packages
 
 
 
2.建立连接
  • # 导包
    • import pymysql
  •  
  • #连接数据库
    • conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',password='1111',database='wangyan')
  • # 创建游标
    • cur=conn.cursor()
  •  
  • #sql语句
    • sql='''
    • # INSERT into score(id,score)
    • # VALUES (7,602)
  •  
    • #update score set score=608 where score=602
    • '''
    • sql="delete from score where id=7"
  •  
  • #执行sql语句
    • cur.execute(sql)
  •  
  • # 提交事务
  •  
  • # 断开连接
    • cur.close()
  • 封装为函数
 
 
 
3.查询(查询不需要提交事务)
  • #连接数据库
    • conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',password='1111',database='wangyan')
  • # 创建游标
    • cur=conn.cursor()
  •  
    • sql="select * from score "
  •  
  • #执行sql语句
    • rest=cur.execute(sql)
    • print(rest)
  •  
  • # 提交事务
  •  
  • #获取数据
    • 方法一
      • print(cur.fetchone())          第一条数据
      • print(cur.fetchone())          第二条数据
      • print(cur.fetchone())          第三条数据
    • 方法二
      • print(cur.fetchall())            全部数据(二维元组)
    • 方法三
      • print(cur.fetchmany(4))      默认为一条数据((数据1,数据2),),对应条数据(二位元组)
  •  
  • # 断开连接
    • cur.close()
    • conn.close()
 
 
posted @ 2020-01-09 11:10  Nobility  阅读(833)  评论(0编辑  收藏  举报