首页 > 数据库 >【八】MySQL数据库之数据库IDE与pymysql模块

【八】MySQL数据库之数据库IDE与pymysql模块

时间:2023-06-30 20:44:05浏览次数:41  
标签:数据库 pymysql cursor sql print IDE root conn

【八】MySQL数据库之数据库IDE与pymysql模块

数据库IDE与pymysql模块

【一】IDE工具介绍

  • 生产环境还是推荐使用mysql命令行
    • 但为了方便我们测试
    • 可以使用IDE工具
  • 在此我们推荐使用Navicat软件或pycharm来连接数据库
    • 这样就能更详细直观地查询数据
掌握:
#1. 测试+链接数据库
#2. 新建库
#3. 新建表,新增字段+类型+约束
#4. 设计表:外键
#5. 新建查询
#6. 备份库/表
#注意:
批量加注释:ctrl+?键
批量去注释:ctrl+shift+?键

【二】pymysql模块

pip3 install pymysql

【1】链接、执行sql、关闭(游标)

import pymysql
user=input('用户名: ').strip()
pwd=input('密码: ').strip()

# 链接
conn=pymysql.connect(host='localhost',user='root',password='123',database='db1',charset='utf8')

# 游标
cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示
#cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)


# 执行sql语句
sql='select * from userinfo where name="%s" and password="%s"' %(user,pwd) #注意%s需要加引号
print(sql)
res=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目
print(res)

cursor.close()
conn.close()

if res:
    print('登录成功')
else:
    print('登录失败')

【2】execute()之sql注入

  • 注意:
    • 符号–会注释掉它之后的sql
    • 正确的语法:
      • –后至少有一个任意字符
  • 根本原理:
    • 就根据程序的字符串拼接name='%s'
    • 我们输入一个xxx' -- haha
    • 用我们输入的xxx加’在程序中拼接成一个判断条件name='xxx' -- haha'
  • 最后那一个空格
    • 在一条sql语句中如果遇到select * from t1 where id > 3 -- and name='egon';
    • 则--之后的条件被注释掉了
#1、sql注入之:用户存在,绕过密码
egon' -- 任意字符

#2、sql注入之:用户不存在,绕过用户与密码
xxx' or 1=1 -- 任意字符

  • 解决方法:
# 原来是我们对sql进行字符串拼接
# sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)
# print(sql)
# res=cursor.execute(sql)

#改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)
sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
res=cursor.execute(sql,[user,pwd]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。

【3】增、删、改:conn.commit()

import pymysql
# 链接
conn=pymysql.connect(host='localhost',user='root',password='123',database='egon')
# 游标
cursor=conn.cursor()

# 执行sql语句
# part1
# sql='insert into userinfo(name,password) values("root","123456");'
# res=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数
# print(res)

# part2
# sql='insert into userinfo(name,password) values(%s,%s);'
# res=cursor.execute(sql,("root","123456")) #执行sql语句,返回sql影响成功的行数
# print(res)

# part3
sql='insert into userinfo(name,password) values(%s,%s);'
res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) #执行sql语句,返回sql影响成功的行数
print(res)

conn.commit() # 提交后才发现表中插入记录成功
cursor.close()
conn.close()

【4】批量查询:fetchone,fetchmany,fetchall

import pymysql
# 链接
conn=pymysql.connect(host='localhost',user='root',password='123',database='egon')
# 游标
cursor=conn.cursor()

# 执行sql语句
sql='select * from userinfo;'
rows=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数rows,将结果放入一个集合,等待被查询

# cursor.scroll(3,mode='absolute') # 相对绝对位置移动
# cursor.scroll(3,mode='relative') # 相对当前位置移动
res1=cursor.fetchone()
res2=cursor.fetchone()
res3=cursor.fetchone()
res4=cursor.fetchmany(2)
res5=cursor.fetchall()
print(res1)
print(res2)
print(res3)
print(res4)
print(res5)
print('%s rows in set (0.00 sec)' %rows)



conn.commit() # 提交后才发现表中插入记录成功
cursor.close()
conn.close()

'''
(1, 'root', '123456')
(2, 'root', '123456')
(3, 'root', '123456')
((4, 'root', '123456'), (5, 'root', '123456'))
((6, 'root', '123456'), (7, 'lhf', '12356'), (8, 'eee', '156'))
rows in set (0.00 sec)
'''

【5】获取插入的最后一条数据的自增ID

import pymysql
conn=pymysql.connect(host='localhost',user='root',password='123',database='db')
cursor=conn.cursor()

sql='insert into userinfo(name,password) values("xxx","123");'
rows=cursor.execute(sql)
print(cursor.lastrowid)  # 在插入语句后查看

conn.commit()

cursor.close()
conn.close()

标签:数据库,pymysql,cursor,sql,print,IDE,root,conn
From: https://www.cnblogs.com/dream-ze/p/17517779.html

相关文章

  • idea的使用技巧
    查找快捷键介绍Ctrl+F在当前文件进行文本查找Ctrl+R在当前文件进行文本替换Shift+Ctrl+F在项目进行文本查找Shift+Ctrl+R在项目进行文本替换Shift+Shift快速搜索Ctrl+N查找classCtrl+Shift+N查找文件Ctrl+Shift+Alt+......
  • IDEA工具使用
    Ctrl+Alt+h表示查看当前方法的实现类或者说当前类的继承关系Ctrl+Alt+b表示当前方法有哪些实现,查看类或方法中的实现,或者使用Ctrl+Alt+鼠标左键......
  • VideoCapture
    fromxgoeduimportXGOEDUimporttime#实例化eduXGO_edu=XGOEDU()XGO_edu.lcd_text(50,50,'hello',color=(255,0,0),fontsize=50)time.sleep(2)importcv2importnumpyasnp#打开摄像头cap=cv2.VideoCapture(0)cap.set(3,320)cap.set(4,240)whi......
  • 2023.6.30//关于java链接SQLserver数据库报错:驱动程序无法通过使用安全套接字层(SSL)
    详情如下:驱动程序无法通过使用安全套接字层(SSL)加密与SQLServer建立安全连接。错误:“PKIXpathbuildingfailed:sun.security.provider.certpath.SunCertPathBuilderException:unabletofindvalidcertificationpathtorequestedtarget”。ClientConnectionId:32d1......
  • 编译python为可执行文件遇到的问题:使用python-oracledb连接oracle数据库时出现错误:DP
    错误原文:DPY-3010:connectionstothisdatabaseserverversionarenotsupportedbypython-oracledbinthinmode链接数据库方式如下:connection=create_engine("oracle+oracledb://user:password@host:post/dbname") PyCharm编译器内运行成功但编译后会有DP......
  • 用友:时序数据库要更懂业务场景
    本文来自IT168作者卢敏 时序数据库是针对时间戳或时间序列数据优化的数据库。比如工业企业为了管好工业设备,需要用传感器收集一些带有时间标签的数据,这些数据既要求“超大规模数据瞬时写入”,又要求实现乱序管理。用友网络助理总裁何冠宇表示,时序数据库的技术发展与上层应用的......
  • SQLite4Unity3d unity游戏数据库
      SQLite4Unity3d是一种基于sqlite-net进行封装的unity包,适用在iOS,Mac,Android和Windows项目中工作,适用于中大型游戏 项目架构传统unity序列化数据共有几种方案,包括使用本地持久化类PlayerPrefs,使用Json方法、使用XMl方法,但以上几种方案有使用上限制,修改自由化,规模化使用......
  • 数据库SqlServer迁移PostgreSql实践
    SqlServer属于商业数据库,不可能像Mysql等数据库一样,去解析相关的数据库binlog,从而实现增量数据的回放,结合应用属性,最后确定采用离线迁移方式,从SqlServer中将表数据全部读出,然后将数据写入到pg中,采用此种方案的弊病就是程序端需停止写入(应用可将部分数据缓存到本......
  • JetBrains系IDE使用Git很慢的问题
    起因:在公司电脑上使用 IntelliJIDEA 的时候发现操作Git特别的慢,status、fetch、pull、checkout、commit等基础操作都执行的特别慢,下方的Task进度条一直处于等待中,等待差不多10秒多的时候才开始执行进度。最难以忍受的是,在Settings里面检查Git的版本操作都需要接近20多秒的时间,......
  • python连接mysql数据库
    连接mysql方式很多,这里先只介绍pymysql库连接mysql数据库。1.安装pymysqlpipinstallpymysql-ihttps://pypi.tuna.tsinghua.edu.cn/simple2.  建立mysql数据表 安装好mysql数据库之后,建立表并插入数据后如下: 表的结构:3.连接数据库连接数据......