随笔 - 41,  文章 - 0,  评论 - 5,  阅读 - 75327

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()
复制代码

 

posted on   琦妮  阅读(2039)  评论(1编辑  收藏  举报
编辑推荐:
· 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月简报

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示