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()
原文地址:https://www.cnblogs.com/mys6/p/11278758.html
时间: 2024-10-10 01:42:51