Python3 MySQL
2024-10-19 21:36:38
首先安装pymysql pip install pymysql
准备数据库:创建一个数据库testdb
mysql实例:
import pymysql #打开数据库连接,使用数据库所在的IP127.0.0.1,数据库的用户名和密码,要操作的数据库testdb
db=pymysql.connect('127.0.0.1','root','','testdb') #使用cursor()创建一个游标对象
cursor=db.cursor() #使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute('drop table if exists user;') #创建表的SQL语句
sql='''create table user (
id int(10) primary key,
user varchar(20)
);
''' #执行sql语句
cursor.execute(sql) #插入数据的SQL语句
sql1="insert into user values(1,'tangqiu');" try:
#执行SQL语句
cursor.execute(sql1)
db.commit()
except :
#发生错误时回滚
db.rollback() #查询的SQL语句
sql2='select * from user;'
#执行SQL语句
cursor.execute(sql2) # 获取所有记录列表
result=cursor.fetchall() print(result)
db.close()
最新文章
- tomcat 7 WARNING: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
- Jquery 获取 radio/select选中值
- [主页]大牛系列01:Microsoft Research的Johannes Kopf
- 开源数据库连接池proxool
- 【Android学习】调用系统短信、电话
- 源代码分析:LayoutParams的wrap_content, match_parent, 而详细的价值观
- iOS开发——打开手机相册,获取图片
- vue工程利用pubsub-js实现兄弟组件之间的通信
- xml方式封装数据方法
- java json转换(一)
- vscode隐藏运行ts生成的js文件和map文件
- Android实现夜间模式
- java通过传入的日期,获取所在周的周一至周日
- Android-Kotlin-具名参数
- 站在巨人肩上的.NET Core 2.1
- AngularJS入门基础——表单验证
- 目标检测之2015iccv---objdetection 专题论文
- 20165211 获奖感想及java课程总结
- RN 中 Native 模块的注入过程
- TensorFlow架构与设计:概述
热门文章
- OpenFOAM——梯形腔双边驱流
- jmap -heap 查看堆内存
- Linux系统学习(二)一Linux基本操作
- Intellij IDEA 智能补全的 10 个姿势,太牛逼了。。
- Centos7安装Redis5.0.5并加入Systemd服务
- nodejs爬虫如何设置动态ip以及userAgent
- python3新特性函数注释Function Annotations用法分析
- Java JDBC利用反射技术将查询结果封装为对象
- Python3基础 tuple list转为tuple
- LIST<;>;泛型集合取得对象的属性值