前言
我们在Python中操作数据库,相信很多小伙伴都是使用pymysql模块进行操作,小龙今天也简单记录一下学习操作增删改查。
一、增:insert
增删改中都需要提交事务:commit()
insert into 表名(字段1,字段2,,字段x)values(字段1值,字段2值,字段x值);
1、sql插入单条数据:execute()
# 1、sql插入数据 use = 'gsxl' pwd = '123456' import pymysql conn =pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='dba') cursor = conn.cursor() sql = "insert into userinfo(user,password)values(%s,%s);" r = cursor.execute(sql,(use,pwd)) conn.commit() # commit:增、删、改都需要提交事务 cursor.close() conn.close() print('受影响的行数:%s'%r)
2、插入多行数据:executemany
# 2、插入多行数据:executemany import pymysql conn =pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='dba') cursor = conn.cursor() sql = "insert into userinfo(user,password)values(%s,%s);" r = cursor.executemany(sql,[('asd','123'),('das','456')]) print('受影响的行数:%s'%r) # 那返回值 conn.commit() # commit:增、删、改都需要提交事务 cursor.close() conn.close()
3、拿自增ID:lastrowid
use = 'gsxl' pwd = '123456' import pymysql conn =pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='dba') cursor = conn.cursor() sql = "insert into userinfo(user,password)values(%s,%s);" cursor.execute(sql,(use,pwd)) r = cursor.lastrowid print('自增ID:%s'%r) conn.commit() cursor.close() conn.close()
终端打印结果:自增ID:31
二、删:delete
1、删除:delete,delete from 表名 where 字段='字段值';
将 user=gsxl 的行删除。
# 1、删除:delete import pymysql conn =pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='dba') cursor = conn.cursor() sql = "delete from userinfo where user='gsxl';" r = cursor.execute(sql) print('受影响的行数:%s'%r) # 拿返回值 conn.commit() # commit:增、删、改都需要提交事务 cursor.close() conn.close()
三、改:update
update 表名 set 要修改的字段=改为什么? where 约束字段条件='值';
# 3、修改:update import pymysql conn =pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='dba') cursor = conn.cursor() sql = "update userinfo set `password`='666' where `user`='gsxl';" r = cursor.execute(sql) print('受影响的行数:%s'%r) # 拿返回值 conn.commit() # commit:增、删、改都需要提交事务 cursor.close() conn.close()
四、查:select
cursor 中 加这个参数能返回有字段名,列表嵌套字典:cursor=pymysql.cursors.DictCursor
fetchone:只拿返回的一条数据,可连续拿数据。
fetchall:拿全部的数据,常与 limit 进行分页使用。
1、查询 user='gsxl' 的数据:
import pymysql conn =pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='dba') cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = "select * from userinfo where user=%s;" cursor.execute(sql,('gsxl')) # result = cursor.fetchone() # 返回一条结果 result = cursor.fetchall() # 返回全部结果 print(result) cursor.close() conn.close()
终端打印结果: [{'id': 27, 'user': 'gsxl', 'password': '666'}, {'id': 28, 'user': 'gsxl', 'password': '666'}, {'id': 29, 'user': 'gsxl', 'password': '666'}]
2、登录小例子:
# 2、sql查询账号密码,登录例子 import pymysql user=input('username:') pwd=input('password:') conn =pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='123456', db='dba') cursor = conn.cursor() sql = "select * from userinfo where user=%s and password=%s;" cursor.execute(sql,(user,pwd)) # (user,pwd):传%s里面的变量,防止sql注入 result = cursor.fetchone() # 返回一条结果 # result = cursor.fetchall() # 返回全部结果 cursor.close() conn.close() if result: print('登录成功') else: print('登录失败')
简单学习下pymysql的使用,总结:
- 有连接就有关闭,游标操作;
- 增删改必须要提交事务:commit();
- 查询则是拿查询返回结果;
欢迎来大家QQ交流群一起学习:482713805