pymysql 模块
import pymysql
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='db1',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor
)
cursor = conn.cursor()
res = cursor.execute('select * from emp;')
print(cursor.fetchone())
print(cursor.fetchone())
cursor.scroll(5, 'absolute')
print(cursor.fetchone())
pymysql 补充
import pymysql
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='db1',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor,
autocommit=False
)
cursor = conn.cursor()
"""
补充知识:
对数据库的增删改是非常严肃的问题,使用需要人为再确认才能真正生效!-> conn.commit() # 提交操作 或者 开启自动提交操作模式
而对应查操作,直接执行!
"""
sql 注入问题
import pymysql
conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='123',
database='db1',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor
)
cursor = conn.cursor()
while 1:
usename = input('名字>>>')
id = input('ID>>>')
sql = "select * from emp where name=%s and id=%s"
rows = cursor.execute(sql, (usename, id))
if rows:
print('登录成功!')
else:
print('登录失败')