学的是基础 所以用的是 一个Sublime Test的 文本进行敲Python 语言
首先 第一步
import pymysql
如果报错 说明没连接 只是测试
cmd打开黑窗口
输入 pip innstall pymysql
输入之前检查是否来连接网络
输入之后 将进行下载 等到 数据下载完成就可以使用了
测试 是否连接
conn=p.connect(host=“127.0.0.1”,user=“root”,passwd=“tiger”,db=“ct”)
print(conn)
host:可以输入自己的ip地址 也可以输入我这个
user:连接数据库的名称
passwd:连接数据库的密码
db:连接数据库表名
#查询
查询前需要获取游标 才能进行查询
c=conn.cursor()
print©
报错代表你输入有误 conn为连接的名称
单个查询
getone=“select * from stu where id=1”
c.execute(getone)
person=c.fetchone()
print(person[1])
多个查询
getMoney=“select * from stu”
c.execute(getMoney)
persons=c.fetchall();
for person in persons:
print(person)
增
insertSQL=“insert into stu values(null,‘王八’,‘28’)”
c.execute(insertSQL)
conn.commit()
conn.close()
每进行一个新增修改 需要关闭他的conn 也就是上面连接的数据库
这样以防万一丢失数据
删除 修改 这里和上面数据一样
对数据库进行