Python连接MySQL数据库之pymysql模块使用
Posted mys6
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python连接MySQL数据库之pymysql模块使用相关的知识,希望对你有一定的参考价值。
01Python登录.py
username = input("输入用户名:")
pwd = input("请输入密码:")
# if username == "erge" and pwd == "dashabi":
# print("登陆成功!")
# else:
# print("滚~")
with open("userinfo.txt", "r", encoding="utf-8") as f:
for line in f:
# print(line.strip())
u, p = line.strip().split("|")
if u == username and p == pwd:
print("登陆成功!")
break
else:
print("go out~")
02Python连接数据库登录.py
import pymysql
username = input("输入用户名:")
pwd = input("请输入密码:")
# if username == "erge" and pwd == "dashabi":
# print("登陆成功!")
# else:
# print("滚~")
# 拿到用户输入的用户名密码
# 去数据库里面判断用户名和密码是否正确
# 1. 连接数据库
conn = pymysql.connect(
host="localhost",
port=3306, # 端口号是数字类型
database="userinfo", # 写自己本地的数据库名字
user="root",
password="123456",
charset="utf8" # 千万记得没有-
)
cursor = conn.cursor() # 获取输入SQL语句的光标对象
sql = "select * from info;"
ret = cursor.execute(sql)
print(ret)
# 关闭连接
cursor.close()
conn.close()
# 2. 判断 --> 只需要把检索条件写到sql语句中,去数据库执行就可以了
# with open("userinfo.txt", "r", encoding="utf-8") as f:
# for line in f:
# # print(line.strip())
# u, p = line.strip().split("|")
# if u == username and p == pwd:
# print("登陆成功!")
# break
# else:
# print("go out~")
03登录校验MySQL.py
import pymysql
# 获取用户输入
username = input("输入用户名:")
pwd = input("请输入密码:")
# 连接数据库检索有没有该用户
conn = pymysql.connect(
host="localhost",
port=3306,
database="userinfo",
user="root",
password="123456",
charset="utf8"
)
cursor = conn.cursor() # 获取光标
# 拼接要执行的SQL语句
sql = "select * from info where username=‘%s‘ and password=‘%s‘" % (username, pwd)
print(sql)
print("=" * 120)
# 执行SQL语句
ret = cursor.execute(sql)
if ret:
print("登录成功")
else:
print("登录失败!")
# 关闭光标对像
cursor.close()
# 关闭连接
conn.close()
04登录校验MySQL规避SQL注入.py
import pymysql
# 获取用户输入
username = input("输入用户名:")
pwd = input("请输入密码:")
# 连接数据库检索有没有该用户
conn = pymysql.connect(
host="localhost",
port=3306,
database="userinfo",
user="root",
password="123456",
charset="utf8"
)
cursor = conn.cursor() # 获取光标
# 拼接要执行的SQL语句
sql = ‘select * from info where username=%s and password=%s‘
print(sql)
print("=" * 120)
# 执行SQL语句
ret = cursor.execute(sql, [username, pwd]) # 让pymysql帮我们拼接SQL语句
if ret:
print("登录成功")
else:
print("登录失败!")
# 关闭光标对像
cursor.close()
# 关闭连接
conn.close()
05pymysql增操作.py
"""
pymysql增操作
"""
import pymysql
conn = pymysql.connect(
host="localhost",
port=3306,
database="userinfo",
user="root",
password="123456",
charset="utf8"
)
cursor = conn.cursor()
# 拼接语句
sql = "insert into info (username, password)VALUES (%s, %s)"
# 执行
try:
cursor.execute(sql, ["大旭",])
# 自己写个for循环 (今天作业自己试下)
conn.commit()
except Exception as e:
print("报错啦:",str(e))
conn.rollback() # 回滚
# 对数据库做写操作一定要记得提交assword
cursor.close()
conn.close()
06演示lastrowid有什么用.py
import pymysql
conn = pymysql.connect(
host="localhost",
port=3306,
database="userinfo",
user="root",
password="123456",
charset="utf8"
)
cursor = conn.cursor()
# 创建班级的sql语句
sql1 = "insert into class (name) VALUES (%s)"
# 创建学生的sql语句
sql2 = "insert into student (name, cid) VALUES (%s, %s)"
cursor.execute(sql1, "全栈9期")
new_id = cursor.lastrowid # 获取刚插入数据的ID值
cursor.execute(sql2, ["小东北", new_id])
conn.commit()
cursor.close()
conn.close()
07批量插入数据.py
import pymysql
conn = pymysql.connect(
host="localhost",
port=3306,
database="userinfo",
user="root",
password="123456",
charset="utf8"
)
cursor = conn.cursor()
# 创建班级的sql语句
sql = "insert into info (username, password) VALUES (%s, %s)"
data = [("alex1", "dashabi"), ("污Sir1",), ("xiaoyima1", "nvshen")]
try:
cursor.executemany(sql, data) # 内部实现for循环,批量执行插入语句
# for i in data:
# cursor.execute(sql, i)
conn.commit() # 提交一次
except Exception as e:
print("错啦!")
conn.rollback()
cursor.close()
conn.close()
08删除数据操作.py
import pymysql
conn = pymysql.connect(
host="localhost",
port=3306,
database="userinfo",
user="root",
password="123456",
charset="utf8"
)
cursor = conn.cursor()
# sql = "delete from info WHERE username=%s"
sql = "delete from info WHERE id=%s"
cursor.execute(sql,8)
conn.commit()
cursor.close()
conn.close()
以上是关于Python连接MySQL数据库之pymysql模块使用的主要内容,如果未能解决你的问题,请参考以下文章