python操作MySQL
一、pymysql模块
1.pymysql基本操作
| import pymysql |
| |
| |
| |
| conn = pymysql.connect( |
| host='127.0.0.1', |
| port=3306, |
| user='root', |
| passwd='root123', |
| db='db3', |
| charset='utf8mb4', |
| |
| ) |
| """ |
| 由于增、删、改、查中,查只读的一个操作的等级比较低,所有不需要二次确认, |
| 而增、删、改的写入的操作,权限等级比较高,所有需要commit()方法来二次确认 |
| 而在连接服务端时候,用autocommit自动提交则不需要二次确认 |
| """ |
| |
| |
| |
| cursor = conn.cursor() |
| """ |
| (('t1',), ('t2',), ('t3',), ('t4',), ('t5',), ('t6',), ('t7',), ('t8',)) |
| """ |
| |
| |
| """ |
| [{'Tables_in_db3': 't1'}, {'Tables_in_db3': 't2'}, {'Tables_in_db3': 't3'}, {'Tables_in_db3': 't4'}, {'Tables_in_db3': 't5'}, {'Tables_in_db3': 't6'}, {'Tables_in_db3': 't7'}, {'Tables_in_db3': 't8'}] |
| """ |
| |
| sql = 'show tables' |
| |
| |
| affect_rows = cursor.execute(sql) |
| print(affect_rows) |
| |
| res = cursor.fetchall() |
| print(res) |
2.增删改查
| import pymysql |
| |
| |
| conn = pymysql.connect( |
| host='127.0.0.1', |
| port=3306, user='root', |
| passwd='root123', |
| db='db4', |
| charset='utf8' |
| ) |
| |
| cursor = conn.cursor() |
| """ |
| 指定参数为 列表内套字典 [{},{}] |
| cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) |
| """ |
| |
| |
| |
| cursor.execute("create database db4 default charset utf8 collate utf8_general_ci") |
| conn.commit() |
| |
| |
| cursor.execute("show databases") |
| cursor.execute("use db4") |
| |
| cursor.execute("show tables") |
| result = cursor.fetchall() |
| conn.commit() |
| |
| |
| cursor.execute("drop database db4") |
| conn.commit() |
| |
| |
| |
| cursor.execute("use mysql") |
| cursor.execute("show tables") |
| res = cursor.fetchall() |
| print(res) |
| |
| |
| cursor.close() |
| conn.close() |
二、pymysql补充说明
1.获取数据
关键字 |
作用 |
fetchall() |
获取所有的结果 |
fetchone() |
获取结果集的第一个数据 |
fetchmany() |
获取指定数量的结果集 |
2.增删改查
关键字 |
作用 |
autocommit=True |
在connect 添加配置,增 删 改 操作时自动确认 |
conn.commit() |
增 删 改 操作时手动添加代码确认 |
三、注册登录
1.注册
| import pymysql |
| def register(): |
| print('用户注册') |
| |
| username = input('请输入用户名:').strip() |
| pwd = input('请输入密码:').strip() |
| |
| conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root123', charset='utf8', db='usersdb') |
| cursor = conn.cursor() |
| |
| sql_statement = "insert into users(name,password) values('{}','{}') ".format(username, pwd) |
| |
| |
| cursor.execute(sql_statement) |
| conn.commit() |
| |
| |
| cursor.close() |
| conn.close() |
| |
| print('注册成功') |
| |
2.登录
| def login(): |
| print('用户登录') |
| |
| |
| username = input('请输入用户名:') |
| pwd = input('请输入密码:') |
| |
| |
| conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root123', db='usersdb') |
| cursor = conn.cursor() |
| |
| |
| |
| sql_statement = "select * from users where name='%s' and password='123'" % (username) |
| |
| |
| cursor.execute(sql_statement) |
| |
| |
| |
| res = cursor.fetchone() |
| |
| |
| cursor.close() |
| conn.close() |
| |
| |
| if res: |
| print('登录成功', res) |
| else: |
| print('登录失败', res) |
注意:SQL注入的问题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY