栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python办公自动化学习笔记--Mysql

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python办公自动化学习笔记--Mysql

1.增删改查(CRUD)

前提:要有mysql的环境

首先安装pymysql,win+r输入cmd:pip install pymysql

import pymysql
database=pymysql.connect(host="127.0.0.1",user="root",password="root",db="manage",charset="utf8")
#注意connect中的格式,版本较低的可能不用写host=、user=、password=和db=
cursor=database.cursor()  #初始化指针
​
#增加
'''
sql="insert into m_user(uid,username,age,telephone,password,sex)values('1868250133','lisa',25,15161151871,'lisa123','男')"
​
cursor.execute(sql)
database.commit()
database.close()
'''
#修改
'''
sql="update m_user set age=100 where uid=1868250133"
cursor.execute(sql)
database.commit()
database.close()
'''
#查询
'''
sql="select * from m_user where sex='男'"
cursor.execute(sql)
result=cursor.fetchall()
print(result)
'''
​
#删除
sql="delete from m_user where uid=1868250133"
cursor.execute(sql)
database.commit()
database.close()

*注意:连接的时候注意写法,低版本的pymysql可能不需要写前缀

凡是对数据库有更改的操作(增删改),一定要执行commit,否则不报错但是数据库中看不到修改

查询的时候要想看到结果,则需要调用fetchall方法,只需要看到某些内容可以把*换成属性名

(对数据库操作不熟悉的推荐看一看mysql必知必会,里面介绍了各种sql语句)

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/878745.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号