首先咱们知道pymysql 是python中操做数据库的模块python
1.与数据库服务器创建连接 conn=pymysql.Connect(....)mysql
2.获取游标对象(用于发送和接受数据)cursor=conn.cursor()sql
3.使用游标执行sql语句cursor.excute(sql)-->此时返回的是执行该语句后数据库表中受影响的数据条数数据库
4.使用fetch方法来获取执行的结果安全
5.关闭链接:先关闭游标,再关闭链接服务器
id | caption |
---|---|
1 | 三年级二班 |
2 | 三年级三班 |
3 | 一年级二班 |
4 | 二年级九班 |
import pymysql conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='root', database='day41', charset='utf8' ) cursor = conn.cursor(pymysql.cursors.DictCursor) # 将查询结果作成字典格式 res = cursor.execute('select * from class') # 返回的值是受影响数据的条数 print(res) # 查询到的数据总条数 print(cursor.fetchone()) # 获取一条查询结果 # cursor.scroll(1, 'absolute') # 绝对移动:相对于文件起始位置,向后移动一组数据 # cursor.scroll(1,'relative') # 相对移动,相对于当前位置,向后移动一个位置 cursor.scroll(-1) # 游标 相对移动到当前位置的前一条数据位置 # 获取全部剩余的内容查询的内容的字典 --》相似于文件读写的游标 print(cursor.fetchall()) # [{'cid': 2, 'caption': '三年三班'}, {'cid': 3, 'caption': '一年二班'}, {'cid': 4, 'caption': '二年九班'}] cursor.close() # 关闭游标 conn.close() # 关闭链接
上面所示的是咱们在python中pymysql的最简单的用法,在实际中,咱们在对数据库的读取操做的时候,是不须要进行commit提交处理的,可是在对表的增和改操做的时候,数据库默认是启用事务的,sql语句若是没有提交至关于没有执行。fetch
id | name | password | money |
---|---|---|---|
1 | qzk | 123 | 1000 |
2 | qby | 123 | 1000 |
3 | qwe | 123 | 1000 |
4 | zdc | 123 | 1000 |
5 | qqq | 123 | 1000 |
# -*- coding: utf-8 -*- import pymysql # Connect 类 实例化一个 conn链接对象 conn = pymysql.Connect( host='127.0.0.1', user='root', port=3306, password='root', database='day41', charset='utf8' ) # 获取游标对象 cursor = conn.cursor(pymysql.cursors.DictCursor) # 这里演示一下 rollback()做用:当有两个或多个操做因其余缘由只完成部分操做而未能执行彻底部操做致使的数据不安全问题,能够对表数据中的增改进行撤销处理。 try: res1 = cursor.execute("update userinfo set money = money-50 where name= 'qqq'") print(cursor.fetchall()) res = cursor.execute("update userinfo set money =money-50 where name= 'ewr'") if not res: # 该处演示当 res 返回值为0时,表示该操做未对数据库产生影响,由于ewr不在数据库中,因此返回0,此事上一句操做 res1 已操做完毕,用户帐户余额已减去50 ,故,须要撤销上面的操做,因此用 rollback() print('转帐失败') conn.rollback() except: conn.rollback() cursor.execute("select * from userinfo") # 执行语句查看数据库的userinfo全部字段信息,返回收影响的数据条数 print(cursor.fetchall()) # 查看到上面语句执行的结果 # 提交修改 由于pymysql模式默认是启用事务的,sql语句若是没提交至关于没执行 conn.commit() # 提交事务 """ 注意:pymysql 默认不提交修改的,可是这里指的是对表中的记录操做不提交,像诸如删库、删表的操做时没法撤销的 """
# -*- coding: utf-8 -*- import pymysql # 建立链接获得一个链接对象 conn = pymysql.Connect( host='127.0.0.1', # 数据库服务器主机地址 port=3306, # 端口号,可选 user='root', # 数据库用户名 password='root', # 数据库密码 database='day41', # 数据库名称 charset='utf8' # 编码 ) while True: name_inp = input('>>>username:').strip() pwd_inp = input('>>>>pwd:').strip() # 获取游标对象 cursor = conn.cursor(pymysql.cursors.DictCursor) sql = 'select * from userinfo where name=%s and password=%s' # cursor 执行sql res = cursor.execute(sql, (name_inp, pwd_inp)) print(res) # sql注入 if res: print('登陆成功') print(cursor.fetchone()) # 查询一个 break else: print('登陆失败,用户名或密码错误') # 关闭游标 cursor.close() # 关闭链接 conn.close() """ 通常咱们不要本身手动拼接(关键参数)查询条件(会致使sql注入) """
# 不要手动去拼接查询的sql语句 username = input(">>>:").strip() password = input(">>>:").strip() sql = "select * from user where username='%s' and password='%s'"%(username,password) # 用户名正确 username >>>: jason' -- jjsakfjjdkjjkjs # 用户名密码都不对的状况 username >>>: xxx' or 1=1 --asdjkdklqwjdjkjasdljad password >>>: ''
# 增 sql = "insert into user(username,password) values(%s,%s)" rows = cursor.excute(sql,('jason','123')) # 修改 sql = "update user set username='qzksdb' where id=1" rows = cursor.excute(sql) """ 增和改单单执行excute并不会真正影响到数据,须要再执行conn.commit()才能够完成真正的增改 """ # 一次插入多行记录 res = cursor,excutemany(sql,[(),(),()]