pymysql是python中操作mysql的模块
安装mysql pip install pymysql mysql版本:mysql-installer-community-8.0.12.0 基本命令 创建数据库 create database student; 使用数据库 use student; 显示所有表 show tables; import pymysql #1、创建数据库连接对象 connect = pymysql.connect( # host:表示主机地址 # 127.0.0.1 本机ip # 172.16.21.41 局域网ip # localhost (local:本地 host:主机 合在一起为本地主机的意思) # host表示mysql安装的地址 host=“127.0.0.1”, user=“root”, passwd=“123456”, # mysql默认的端口号是3306 port=3306, # 数据库名称 db=“student” ) #2、创建游标,用于操作表 cursor = connect.cursor() 3、创建表 create_table = “create table if not exists stu (name varchar(30), age integer, phone varchar(11))” cursor.execute(create_table) #4、表的增、删、改、查 #增加 insert_table = “insert into stu (name,age,phone) values (‘张三’, 19, ‘13332001256’)” cursor.execute(insert_table) #删除 delete_table = “delete from stu where name=‘张三’” cursor.execute(delete_table) #修改 update_table = “update stu set age = 20 where id < 10” cursor.execute(update_table) #查询 select_table = “select * from stu” res = cursor.execute(select_table) s = res.fetchone() print(s) ss = res.fetchall() print(ss) 5、提交sql语句 connect.commit() 6、关闭游标、数据库 cursor.close() connect.close()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报