MySQL数据库~~~~pymysql 连接 MySQL的客户端
2024-08-31 14:42:17
import pymysql
conn = pymysql.connect(
host = '127.0.0.1', # 主机
port = 3306, # 端口号
user = 'root', # 用户名
password = '123', # 密码
database = 'db1', # 需要连接的库
charset = 'utf8')
cursor = conn.cursor(pymysql.cursors.DictCursor)
# 默认游标取出的数据结构是((),(),)
# DictCursor 对应的数据结构是[{},{},],如果用的是fetchone,那么结果是{}.
sql = 'select * from t1;'
ret = cursor.execute(sql) # ret 受影响的行数
print(ret)
print(cursor.fetchall()) # 查询所有数据
print(cursor.fetchone()) # 查询一条数据
print(cursor.fetchmany(2)) # 查询多条数据,不写参数默认一条
cursor.scroll(2,'absolute') # 绝对移动光标,相对于数据最开始的位置进行光标的移动
cursor.scroll(2,'relative') # 相对移动光标,按照光标当前位置来进行光标的移动
sql = 'insert into t1 values(1,"liky");'
conn.commit() # 增删改都必须进行提交操作commit
import pymysql
conn = pymysql.connct(
host = '127.0.0.1', # 主机
port = 3306, # 端口号
user = 'root', # 用户名
password = '123', # 密码
database = 'db1', # 需要连接的库
charset = 'utf8')
while 1:
username = input('请输入用户名:')
password = input('请输入密码:')
cursor = conn.cursor(pymysql.cursors.DictCursor)
sql = "select * from userinfo where username = '%s' and password = '%s';"%(username,password)
#这样写如果在不知道用户名和密码的情况下,用户名输入'liky--空格,就会登录成功;如果知道用户名不知道密码的情况下,用户名输入'liky--空格也会登陆,sql语句中 --空格 是注释的意思.
#解决sql注入问题:
sql = "select * from userinfo where username = %s and password = %s;"
ret = cursor.execute(sql,[username,password])
if ret:
print('登陆成功!')
break
else:
print('账号或密码错误,请重新输入!')
最新文章
- Logback_日志使用详解(转)
- CSS语法
- 新浪微博客户端(47)-在TextView中插入表情
- 李洪强iOS经典面试题上
- openerp安装记录及postgresql数据库问题解决
- 在Ios里UIWebView参入js
- 47.MIF和COE文件格式
- Tomcat 架构 (一)
- hdu3602(变形背包)
- 从零开始打jar包
- eclipse 用maven创建web项目
- xshell无法登录阿里云服务器
- git日常使用经验积累
- PHP八大设计模式
- Content Security Policy (CSP) 介绍
- <;1>;Python生成高质量Html文件:Pyh模块+Bootstrap框架
- Ps—导出:sql作业配合ps导出csv文件
- parted分区工具用法
- 【刷题】BZOJ 4059 [Cerc2012]Non-boring sequences
- 2.void 0 与 不可靠的undefined
热门文章
- 小程序使用wxs解决wxml保留2位小数问题
- [TimLinux] Python 自定义描述符
- [TimLinux] JavaScript 面向对象程序设计
- poj3111 K Best 最大化平均值,二分
- 2018 ICPC南京网络赛 Set(字典树 + 合并 + lazy更新)
- 新更新的OV7670 OV7725模块效果展示 OV7670 FPC版 30万像素 CMOS模块 兼容官哥方便 FPGA stm32f407 68013等使用
- iSensor APP 之 摄像头调试 OV3640 OV2640 MT9d112 ov5642
- Ubuntu19.10安装
- php数组去重、魔术方法、redis常用数据结构及应用场景
- webpack打包出现WARNING in configuration The &#39;mode&#39; option has not been set, webpack will fallback to &#39;production&#39; for this value. 错误