PyMySQL安装
pip install pymysql
链接数据库
在进行本文如下内容以前须要注意:python
- 你有一个MySQL数据库,而且已经启动。
- 你有能够链接该数据库的用户名和密码
- 你有一个有权限操做的database
基本使用
# 导入pymysql模块 import pymysql # 链接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 获得一个能够执行SQL语句的光标对象 cursor = conn.cursor() # 定义要执行的SQL语句 sql = """ CREATE TABLE USER1 ( id INT auto_increment PRIMARY KEY , name CHAR(10) NOT NULL UNIQUE, age TINYINT NOT NULL )ENGINE=innodb DEFAULT CHARSET=utf8; """ # 执行SQL语句 cursor.execute(sql) # 关闭光标对象 cursor.close() # 关闭数据库链接 conn.close()


# 导入pymysql模块 import pymysql # 链接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 获得一个能够执行SQL语句而且将结果做为字典返回的游标 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 定义要执行的SQL语句 sql = """ CREATE TABLE USER1 ( id INT auto_increment PRIMARY KEY , name CHAR(10) NOT NULL UNIQUE, age TINYINT NOT NULL )ENGINE=innodb DEFAULT CHARSET=utf8; """ # 执行SQL语句 cursor.execute(sql) # 关闭光标对象 cursor.close() # 关闭数据库链接 conn.close()
注意:mysql
charset=“utf8”,编码不要写成"utf-8"sql


""" 获取用户输入,登陆 """ import pymysql # 1. 获取用户输入 name = input('请输入用户名:') pwd = input('请输入密码:') # 判断用户名和密码是否正确 # 去数据库查询一下 用户输入的用户名和密码是否正确 # 在Python程序中要链接数据库执行SQL语句 --> pymysql模块 # 1. 链接数据库,获得一个链接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123', database='day43', charset='utf8' ) # 2. 获取光标 cursor = conn.cursor() # 3. 执行SQL语句 # 3.1 获得SQL语句 sql = "select * from userinfo where username='%s' and password='%s';" % (name, pwd) print(sql) # 3.2 使用光标对象执行SQL语句 ret = cursor.execute(sql) # 关闭 cursor.close() conn.close() # 4 获得结果 if ret: print('登录成功') else: print('登陆失败')
SQL注入问题
1. 什么是SQL注入? 用户输入的内容有恶意的SQL语句,后端拿到用户输入的内容不作检测直接作字符串拼接,获得一个和预期不一致的SQL语句 2. 如何解决SQL注入? 对用户输入的内容作检测 pymysql内置了这种检测,咱们只须要让pymysql帮咱们拼接sql语句 ret = cursor.execute(sql, [name, pwd]) # 让pymysql模块帮咱们拼接sql语句,执行SQL语句


''' 获取用户输入,登陆 ''' import pymysql name = input('请输入用户名:') pwd = input('请输入密码:') # 1.链接数据库,获得一个链接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123', database='day43', charset='utf8' ) # 2.获取光标 cursor = conn.cursor() # 3.执行sql语句 # 3.1 获得sql语句 sql = "select * from userinfo where username=%s and password=%s;" # 按照pymysql模块的写法定义好占位符 print(sql) # 3.2使用光标对象执行sql语句 # ret = cursor.execute(sql) ret = cursor.execute(sql, [name, pwd]) # 让pymsql模块帮咱们拼接sql语句,执行sql语句 # 关闭 cursor.close() conn.close() # 4.获得结果 print(ret) if ret: print('登陆成功') else: print('登陆失败') # with open('userinfo', ) as f: # for line in f: # u,p = line.strip().split() # if u
增
""" 向userinfo表插入一条数据 """ import pymysql # 1. 链接数据库,获得一个链接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123', database='day43', charset='utf8' ) # 2. 获取光标 cursor = conn.cursor() # 3. 执行SQL语句 # 3.1 获得SQL语句 sql = "insert into userinfo(username, password) values (%s,%s);" # 按照pymysql模块的写法定义好占位符 # 3.2 使用光标对象执行SQL语句 ret = cursor.execute(sql, ['Eva_J', '456']) # 让pymysql模块帮咱们拼接sql语句,执行SQL语句 # 涉及操做数据库的 必定要提交 conn.commit() # 关闭 cursor.close() conn.close()
插入数据失败回滚数据库
在执行增删改操做时,若是不想提交前面的操做,可使用 rollback() 回滚取消操做。后端
# 导入pymysql模块 import pymysql # 链接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 获得一个能够执行SQL语句的光标对象 cursor = conn.cursor() sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);" username = "Alex" age = 18 try: # 执行SQL语句 cursor.execute(sql, [username, age]) # 提交事务 conn.commit() except Exception as e: # 有异常,回滚事务 conn.rollback() cursor.close() conn.close()
获取插入数据的ID(关联操做时会用到)ide
# 导入pymysql模块 import pymysql # 链接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 获得一个能够执行SQL语句的光标对象 cursor = conn.cursor() sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);" username = "Alex" age = 18 try: # 执行SQL语句 cursor.execute(sql, [username, age]) # 提交事务 conn.commit() # 提交以后,获取刚插入的数据的ID last_id = cursor.lastrowid except Exception as e: # 有异常,回滚事务 conn.rollback() cursor.close() conn.close()
批量执行fetch
# 导入pymysql模块 import pymysql # 链接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 获得一个能够执行SQL语句的光标对象 cursor = conn.cursor() sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);" data = [("Alex", 18), ("Egon", 20), ("Yuan", 21)] try: # 批量执行多条插入SQL语句 cursor.executemany(sql, data) # 提交事务 conn.commit() except Exception as e: # 有异常,回滚事务 conn.rollback() cursor.close() conn.close()
删
""" 从userinfo表把alex删掉 """ import pymysql # 1. 链接数据库,获得一个链接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123', database='day43', charset='utf8' ) # 2. 获取光标 cursor = conn.cursor() # 3. 执行SQL语句 # 3.1 获得SQL语句 sql = "delete from userinfo where username=%s;" # 按照pymysql模块的写法定义好占位符 # 3.2 使用光标对象执行SQL语句 ret = cursor.execute(sql, ['alex']) # 让pymysql模块帮咱们拼接sql语句,执行SQL语句 # 涉及操做数据库的 必定要提交 conn.commit() # 关闭 cursor.close() conn.close()
改
""" 从userinfo表把金老板的密码改为789 """ import pymysql # 1. 链接数据库,获得一个链接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123', database='day43', charset='utf8' ) # 2. 获取光标 cursor = conn.cursor() # 3. 执行SQL语句 # 3.1 获得SQL语句 sql = "update userinfo set password=%s where username=%s;" # 按照pymysql模块的写法定义好占位符 # 3.2 使用光标对象执行SQL语句 ret = cursor.execute(sql, ['789', 'gold']) # 让pymysql模块帮咱们拼接sql语句,执行SQL语句 # 涉及操做数据库的 必定要提交 conn.commit() # 关闭 cursor.close() conn.close()
查
""" 从userinfo表查询全部数据 """ import pymysql # 1. 链接数据库,获得一个链接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123', database='day43', charset='utf8' ) # 2. 获取光标 cursor = conn.cursor() # 3. 执行SQL语句 # 3.1 获得SQL语句 sql = "select * from userinfo;" # 按照pymysql模块的写法定义好占位符 # 3.2 使用光标对象执行SQL语句 cursor.execute(sql) # 让pymysql模块帮咱们拼接sql语句,执行SQL语句 ret = cursor.fetchall() print(ret) # 关闭 cursor.close() conn.close()
查(字典类型)
""" 从userinfo表查询全部数据 """ import pymysql # 1. 链接数据库,获得一个链接 conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', password='123', database='day43', charset='utf8' ) # 2. 获取光标 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 3. 执行SQL语句 # 3.1 获得SQL语句 sql = "select * from userinfo;" # 按照pymysql模块的写法定义好占位符 # 3.2 使用光标对象执行SQL语句 cursor.execute(sql) # 让pymysql模块帮咱们拼接sql语句,执行SQL语句 # 查询全部 # ret = cursor.fetchall() # 查询单条记录 # ret = cursor.fetchone() # print(ret) # ret = cursor.fetchone() # print(ret) # ret = cursor.fetchone() # print(ret) # 查询指定数量的数据 ret = cursor.fetchmany(3) print(ret) print(cursor.fetchone()) print(cursor.fetchone()) # cursor.scroll(0, mode='absolute') # 绝对位置,你让光标移动到哪里 # cursor.scroll(-1, mode='relative') # 相对位置,基于光标当前位置移动 print(cursor.fetchone()) # 关闭 cursor.close() conn.close()
进阶用法
# 能够获取指定数量的数据 cursor.fetchmany(3) # 光标按绝对位置移动1 cursor.scroll(1, mode="absolute") # 光标按照相对位置(当前位置)移动1 cursor.scroll(1, mode="relative")