一 Navicat

在生产环境中操作MySQL数据库还是推荐使用命令行工具mysql,但在我们自己开发测试时,可以使用可视化工具Navicat,以图形界面的形式操作MySQL数据库

官网下载:https://www.navicat.com/en/products/navicat-for-mysql

网盘下载:https://pan.baidu.com/s/1bpo5mqj

掌握:
#1. 测试+链接数据库

#2. 新建库

#3. 新建表,新增字段+类型+约束

#4. 设计表:外键

#5. 新建查询

#6. 备份库/表

转存SQL文件到电脑里——

#注意:
批量加注释:ctrl+?键
批量去注释:ctrl+shift+?键

二.pymysql模块对数据的增删改查

之前我们都是通过MySQL自带的命令行客户端工具mysql来操作数据库,那如何在python程序中操作数据库呢?这就用到了pymysql模块,该模块本质就是一个套接字客户端软件,使用前需要事先安装

不会装模块的可以看看别人的教程

https://www.cnblogs.com/lsm-boke/p/8849067.html

1.pymysql的基本使用

import pymysql

user =input("用户名:").strip()
pwd = input("密码:").strip() #链接
conn = pymysql.connect(
host="localhost",
user="root",
password="",
db="db3",
charset="utf8"
)
#游标
cursor= conn.cursor() #执行完毕返回的结果集默认以元组显示 #执行SQL语句
# sql = 'select * from userinfo where name = "%s" and pwd="%s"' % (user, pwd)
# res = cursor.execute(sql) # 执行sql语句 #execute()之sql注入
sql = 'select * from userinfo where name = %s and pwd=%s'
res = cursor.execute(sql, (user, pwd)) # 执行sql语句 #execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了 cursor.close()
conn.close() if res:
print("登录成功") else:
print("登录失败")

二.增、删、改:conn.commit()

import pymysql
# 1.增删改 # 建立连接
conn = pymysql.connect(
host ="localhost",
user="root",
password="",
db="db3",
charset="utf8"
)
# 拿游标
cursor = conn.cursor() # 执行sql
# 增
sql = 'insert into userinfo(name,pwd) values(%s,%s)'
rows = cursor.execute(sql, ("ming", "123")) #执行sql语句,返回sql影响成功的行数
# rows = cursor.executemany(sql, [("wxx", "123"), ("egon1", "233"), ("alex1", "324")]) #执行sql语句,返回sql影响成功的行数
# 获取插入的最后一条数据的自增ID
print(cursor.lastrowid) # 在插入语句后查看 ,不插入还不能看 # 删
# sql = 'delete from userinfo where name=%s and pwd=%s'
# rows = cursor.execute(sql, ("alex1", "324")) # 与sql里的表操作大同小异,不过真的要改的话,不如直接去navicat里去改了,方便 # 改
# sql = 'update userinfo set pwd=%s where name=%s'
# rows = cursor.execute(sql, ("144", "egon1"))
conn.commit() #提交后才发现表中插入记录成
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

三 .查:fetchone,fetchmany,fetchall

import pymysql
#2.查询 # 建立连接
# conn = pymysql.connect(
# host ="localhost",
# user="root",
# password="",
# db="db3",
# charset="utf8"
# )
# 拿游标
# cursor = conn.cursor(pymysql.cursors.DictCursor)
# # 执行SQL
# rows= cursor.execute('select * from userinfo') # 执行sql语句,返回sql影响成功的行数rows,将结果放入一个集合,等待被查询
#
# # cursor.scroll(2, mode="absolute") # 相对绝对位置移动
# # cursor.scroll(2, mode="relative") # 相对当前位置移动
#
#
# print(cursor.fetchone())
# cursor.scroll(2, mode="relative") # 相对当前位置移动
# print(cursor.fetchone())
# #
# # print(cursor.fetchmany(2))
# # print(cursor.fetchall())

感谢观看,如有不对,一定要指出。小木在这里谢谢了

最新文章

  1. Apache安装
  2. JSP工作原理
  3. openssl 学习之从证书中提取RSA公钥N 和 E
  4. poi 读取 excel (.xls) 97-2003
  5. 【html】【12】特效篇--轮播图
  6. Javascript之<script>标签
  7. Android在Context详细解释 ---- 你不知道Context
  8. rocketmq(1)
  9. BigDecimal工具类处理精度计算
  10. GSS4 - Can you answer these queries IV(线段树懒操作)
  11. 转载:C# socket端口复用-多主机头绑定
  12. 『TensorFlow』网络操作API_中_损失函数及分类器
  13. Android Studio Fragment 无法获取 id的方法
  14. Sitecore营销自动化
  15. databus编译: Execution failed for task ':databus-core:databus-core-impl:compileJava'.
  16. 20145324 Java实验五
  17. windchill系统安装大概步骤
  18. windows程序查看可以行文件依赖库
  19. python学习笔记(pict+subprocess)
  20. DotNet Core Console 程序使用NLog

热门文章

  1. 按highcharts中column形式转对象展现格式
  2. Error in created hook: "TypeError: Cannot read properties of undefined (reading 'get')"
  3. 聊一下kafka的消费组
  4. mogdb的一主两备
  5. 百度脑图kityminder
  6. java如何将逗号分隔的字符串转成int或者long数组
  7. Git 提交和拉取服务器最新版本代码方法
  8. spring 理念与项目构建
  9. Si24R2F+ 无线发射芯片的主要特性及应用介绍
  10. vue去除富文本的标签和样式