【python3 自动化之mysql操作】python3下的mysql入门基础
1、所需资源:pycharm,python3.6,module:pymysql
2、pycharm配置mysql:
新添加一个mysql数据库
ip:192.168.112.54 端口:3306 账号:root 密码:123456
接下来,创建数据库表信息:(代码改编,来自网络)
/*1、创建表*/ DROP TABLE IF EXISTS mysql.Student; create table Student ( Sno ), Sname ), primary key (Sno) ) ENGINE = InnoDB DEFAULT CHARSET = utf8; DROP TABLE IF EXISTS mysql.Course; create table Course ( Cno ), Cname ), Tno ), primary key (Cno) ) ENGINE = InnoDB DEFAULT CHARSET = utf8; DROP TABLE IF EXISTS mysql.SC; create table SC ( Sno ), Cno ), score int, primary key (Sno, Cno) ) ENGINE = InnoDB DEFAULT CHARSET = utf8; DROP TABLE IF EXISTS mysql.Teacher; create table Teacher ( Tno ), Tname ), primary key (Tno) ) ENGINE = InnoDB DEFAULT CHARSET = utf8; /*2、插入表数据*/ ', '陈一'); ', '郭二'); ', '张三'); ', '李四'); ', '王五'); ', '张老师'); ', '王老师'); ', '钱老师'); ', '刘老师'); ', '胡老师'); '); '); '); '); '); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); ); /*运行sql文件出错,解决办法:使用Notepad++打开文件,选择 格式->无BOM的UTF8格式编码*/ /*2、单表查询_表数据*/ select * from Teacher; select * from Student; select * from Course; select * from SC; /*3、多表查询_表数据*/ select s.Sname as "学生", t.Tname as "教师", c.Cname as "课程", sc.score as "分数" from Student s, SC sc, Course c, Teacher t where sc.Cno = c.Cno and sc.Sno = s.Sno and c.Tno = t.Tno order by s.Sname, t.Tname, c.Cname, sc.score;
输出查询结果【部分截图】,能够正常显示,说明环境配置正常
紧接着:python代码去模拟手工操作数据库
# coding:utf-8 import pymysql ''' 新添加一个mysql数据库 ip:192.168.112.54 端口:3306 账号:root 密码:123456 ''' # 连接mysq数据库 connection = pymysql.connect(host='192.168.112.54', port=3306, user='root', password=', db='mysql', charset='utf8', cursorclass=pymysql.cursors.DictCursor) # 实例化——创建数据库游标 cursor = connection.cursor() # 使用execute方法执行SQL语句 id = ' i = cursor.execute("select * from Student s where s.Sno = %s" % id) print("记录数:%d;" % i, "类型:", type(i)) # 使用 fetchone() 方法获取一条数据 data = cursor.fetchone() print("data:", data, "type:", type(data)) # fetchone()返回字典类型,通过字典访问值 print("Student Number : %s " % data["Sno"]) # 使用 fetchall() 方法获取多条数据 j = cursor.execute("select * from Teacher ") print("记录数:%d;" % j, "类型:", type(j)) datas = cursor.fetchall() # 循环遍历查找字典值 for k in datas: print(k["Tname"]) print("datas:", datas, "type:", type(datas)) # fetchall()返回list类型,通过list访问值 print(datas[0]["Tname"]) # 关闭数据库连接 cursor.close() connection.close()
输出查询结果【部分截图】
数据库修改等基本操作,请查阅mysql相关知识
-----------------------------------------------------------------------------
特别鸣谢以下大佬:
Anges黎梦 博客地址:https://www.cnblogs.com/AngesZhu/
快捷通道:
想学自动化测试(接口自动化、selenium自动化、appium自动化等)请联系,上海悠悠
悠悠大佬博客:
https://www.cnblogs.com/yoyoketang/
雷总博客:
https://www.cnblogs.com/leiziv5/
【python3 自动化之mysql操作】python3下的mysql入门基础的更多相关文章
- python学习道路(day12note)(mysql操作,python链接mysql,redis)
1,针对mysql操作 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass'); 设置密码 update user set password ...
- InnoDB Insert(插入)操作(下)--mysql技术内幕
接上一篇文章,最后做的那个实验,我是想证明mysql innodb存储引擎,commit操作与flush数据到磁盘之间的关系,当与同事交流之后,他说,你应该把innodb_buffer_size的大小 ...
- mysql bin路径下的mysql被杀毒软件查杀后恢复过来也无法启动
mysql服务被杀毒软件干掉之后操作 文件恢复过来后还是无法启动 同事使用杀毒软件之后发现,mysql的服务被干掉了.之后想到了处理办法: mysqld-nt -installnet start my ...
- mac下的一些mysql操作
#一.从终端进入mysql 不同于windows下的mysql.mac下的mysql安装路径不同,所以操作上会略有不同: 以下操作以默认安装mysql为前提. ##一(1):打开终端后,先设置路径,后 ...
- Centos下安装mysql 总结
一.MySQL安装 Centos下安装mysql 请点开:http://www.centoscn.com/CentosServer/sql/2013/0817/1285.html 二.MySQL的几个 ...
- shell执行mysql操作
http://ully.iteye.com/blog/1226494 http://www.jb51.net/article/55207.htm shell执行mysql操作 mysql -hhos ...
- 【转】Shell执行MySql操作
mysql -hhostname -Pport -uusername -ppassword -e 相关mysql的sql语句,不用在mysql的提示符下运行mysql,即可以在shell中操作m ...
- linux的mysql操作
最近工作中经常需要使用到MySQL,有时候在WINXP,有时候在Linux中,而这次,需要在CentOS中配置一下,还需要用到phpmyadmin, 在网上搜了不少的资料. 无意中还找到了CentOS ...
- Linux 下卸载MySQL 5
对于在Linux下通过rpm方式的mysql,我们能够通过移除这些rpm包以及删除项目的文件夹来达到卸载的目的.本文演示了在SUSE Linux 10下下载MySQL 5.5.37.详细见下文. 1. ...
- CentOS7下一个mysql安装
CentOS7安装MySQL --下载mysql http://mirrors.sohu.com/mysql/MySQL-5.6/ http://mirrors.sohu.com/mysql/MySQ ...
随机推荐
- NOIP2002普及组复赛B 选数
题目链接:https://ac.nowcoder.com/acm/contest/230/B 题目大意: 略 分析: DFS模板题. 代码如下: #include <bits/stdc++.h& ...
- @Autowired 基本介绍,有待丰富
今天做项目对于@Autowired理解有点错误:过两天再来看看 转自:https://www.cnblogs.com/szlbm/p/5512931.html 什么是注解 传统的Spring做法是使用 ...
- 二、IIS部署WebApi
一.项目发布 二.hosts 更改 C:\Windows\System32\drivers\etc 三.网站搭建 之后我将端口默认更改 8001 以防与80端口冲突 注意: 1.先测试IIS的lo ...
- MongoDB介绍与安装
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类似 json 的 bson 格式,因此可以存储比较复杂的数据 ...
- hdu 1848 简单SG函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1848 Problem Description 任何一个大学生对菲波那契数列(Fibonacci num ...
- 首次使用Oracle SQL Developer 提示: enter the full pathname for java.exe
https://www.cnblogs.com/520future/p/7699095.html 首次使用Oracle SQL Developer 提示: enter the full pathnam ...
- 项目管理——WBS工作分解法
首先我们要了解什么是WBS工作分解法 工作分解结构(Work Breakdown Structure,简称WBS)跟因数分解是一个原理,就是把一个项目,按一定的原则分解,项目分解成任务,任务再分解成一 ...
- GCC __builtin_expect的作用
https://blog.csdn.net/shuimuniao/article/details/8017971 #define LIKELY(x) __builtin_expect(!!(x), 1 ...
- JavaXML整理
XML 浏览器的入口不同(访问路径),访问的资源也不同. 1.1XML语法 1.文档声明必须为<?xml开头,以?>结束; 2.文档声明必须从文档的0行0列位置开始: 3.文档声明只有属性 ...
- css 选择器符号
1. 空格 —— “后代选择器” 例如下面这个例子,表示div元素里面所有的p元素 div p { ... } 2. > —— “子选择器” 例如下面这个例子,表示div元素里面所有的子代(不含 ...