PDBC详细介绍
1.PDBC
-
利用第三方库pymysql
-
内置库
-
第三方库
-
查看(cmd)
-
pip list
-
工程 project interpreter查看
-
安装
-
命令
-
默认到python官方下载
-
pip install 第三方库的名称
-
如果报错为超时
-
pip --default-timeout=1000 install 库名
-
在python官网无法下载,或者下载速度很慢
-
pip --default-timeout=1000 install 库名 -i 网站地址
-
国内资源:
-
安装指定版本
-
pip install 库名 == 版本号
-
通过离线包安装
-
-
setiing
-
-
卸载
-
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()