标签:cursor username hal user pwd 登录 aos mysql学习 tab
pymysql模块:
pip3 install pymysql -i https://pypi.douban.com/simple
Python模块:对数据库进行操作(SQL语句)
1. Python实现用户登录
2. MySQL保存数据
- 连接、关闭(游标)
- execute() -- SQL注入
- 增删改: conn.commit()
- 查 fetchone fetchall
- 获取插入数据自增ID
m1.py m2.py
import pymysql
user = input("username:")
# sql 注入 username:(asdnaosjd‘ 1=1 -- )
pwd = input("password:")
conn= pymysql.connect(host="127.0.0.1", user= ‘root‘, password=‘123456‘, database=‘day4‘)
cursor = conn.cursor()
sql = "select * from exp1 where username =‘%s‘ and password = ‘%s‘"%(user, pwd,)
cursor.execute(sql)
result= cursor.fetchone()
cursor.close()
conn.close()
if result:
print("successful")
else:
print("fault")
m2.py
import pymysql
user = input("username:")
pwd = input("password:")
conn= pymysql.connect(host="127.0.0.1", user= ‘root‘, password=‘123456‘, database=‘day4‘)
cursor = conn.cursor()
sql = "select * from exp1 where username =‘%s‘ and password = ‘%s‘"
cursor.execute(sql,user, pwd)
result= cursor.fetchone()
cursor.close()
conn.close()
if result:
print("successful")
else:
print("fault")
m3.py
import pymysql
# 增,删,改
# user = ‘123‘
# pwd = ‘123‘
conn= pymysql.connect(host="127.0.0.1", user= ‘root‘, password=‘123456‘, database=‘day4‘)
cursor = conn.cursor()
# sql = "insert into exp1 (username, password ) value (‘ll‘,‘123‘) "
sql = "insert into exp1 (username, password ) value (%s,%s) "
# 受影响的行数
#r = cursor.execute(sql,(user, pwd,))
r = cursor.executemany(sql, [(‘wi‘,‘123‘),(‘wq‘,‘123‘)])
conn.commit()
cursor.close()
conn.close()
# 查
conn= pymysql.connect(host="127.0.0.1", user= ‘root‘, password=‘123456‘, database=‘day4‘)
cursor = conn.cursor(cursor= pymysql.cursors.DictCursor)
sql = "select * from exp1"
cursor.execute(sql)
result = cursor.fetchone()
# print( result)
# result = cursor.fetchall()
# print(result)
cursor.close()
conn.close()
# 新插入数据的自增id
conn= pymysql.connect(host="127.0.0.1", user= ‘root‘, password=‘123456‘, database=‘day4‘)
cursor = conn.cursor()
sql = "insert into exp1(username,password) value (‘1132‘ , ‘1‘)"
cursor.execute(sql)
print(cursor.lastrowid)
conn.commit()
cursor.close()
conn.close()
标签:cursor username hal user pwd 登录 aos mysql学习 tab
原文地址:https://www.cnblogs.com/kangkang1999/p/13232953.html