python操作数据库,, 目录
python操作数据库,, 目录
目录
一、python操作数据库 二、数据注入问题一、python操作数据库
import pymysqlconn = pymysql.connect( host = '127.0.0.1', port = 3306, user = 'root', password = '123', database = 'jeff', charset = 'utf8')cursor = conn.cursor(pymysql.cursors.DictCursor) # 产生游标对象,以字典的形式返回sql = 'select * from teacher'cursor.execute(sql) # 执行传入的sql语句# print(cursor.fetchone()) # 只获取一条数据# print(cursor.fetchone()) # 只获取一条数据# print(cursor.fetchone()) # 只获取一条数据# print(cursor.fetchone()) # 只获取一条数据# cursor.scroll(2,'absolute') # 控制光标移动 absolute相对于其实位置 往后移动几位# cursor.scroll(1,'relative') # relative相对于当前位置 往后移动几位print(cursor.fetchall()) # 获取所有的数据 返回的结果是一个列表
二、数据注入问题
import pymysqlconn = pymysql.connect( host = '127.0.0.1', port = 3306, user = 'root', password = '123', database = 'jeff', charset = 'utf8', autocommit = True # 这个参数配置完成后 增删改操作都不需要在手动加conn.commit了)cursor = conn.cursor(pymysql.cursors.DictCursor)username = input('输入用户名>>>:')password = input('输入密码>>>:')sql = 'select * from user where name = %s and password = %s'res = cursor.execute(sql, (username, password)) # 能够帮你自动过滤特殊符号 避免sql注入的问题if res : print(cursor.fetchall())else: print('用户名或密码错误')
python操作数据库
相关内容
- centos7 下面python2 共存python3,,第一步、查看pyth
- python paramiko的使用介绍,,一:使用parami
- Python 之 类的定义与简单使用,,代码#Author
- Python中读取txt文本出现:SyntaxError: (unicode error) 
- Python初学者入门指南,5天入门Python不是问题(附资料)
- python识别图片,,import req
- Python打包分发工具setuptools,,import sys
- python第三方库 - dateurtil,,简介 扩展并增强
- 用Python可视化股票指标,,用Python可视化
- python数据分析与展示(一),,NumPy库学习一.
评论关闭