pip install pymysql
import pymysql
user = input('请输入用户名请输入密码:').strip()
pwd= input("请输入密码:").strip()
# 建立连接
conn = pymysql.connect(
host = '192.168.1.1',
port = '3306',
user = 'root',
password = '123',
db = 'myTestDB',
charset = 'utf8',
)
# 获取游标
cursor = conn.cursor()
# 执行sql语句
# sql = 'select * from USER_TABLE where user="%s" and pwd=%s' % (user,pwd) 自己拼接sql语句有安全风险
# rows = cursor.excute(sql)
sql = 'select * from USER_TABLE where user="%s" and pwd=%s'
rows = cursor.excute(sql,(user,pwd))
#插入多个值
rows = cursor.excutemany(sql,[(user,pwd),('aaa','123'),('bbb','123')])
conn.commit()
cursor.close()
conn.close()
if rows:
print("登录成功")
else:
print("登录失败")
原文:https://www.cnblogs.com/SebastianHan/p/11437705.html