MySQL数据库之Python 学习笔记 - 操作MySQL
小标 2019-06-24 来源 : 阅读 940 评论 0

摘要:本文主要向大家介绍了MySQL数据库之Python 学习笔记 - 操作MySQL ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

本文主要向大家介绍了MySQL数据库之Python 学习笔记 - 操作MySQL ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

MySQL数据库之Python 学习笔记 - 操作MySQL

Python里面操作MySQL可以通过两个方式:

  1. pymysql模块

  2. ORM框架的SQLAchemey


本节先学习第一种方式。


简单的回顾一下基本环境的搭建:


首先安装Mariadb(我的环境是CentOS7)

yum install mariadb*
systemctl start mariadb

配置防火墙

firewall-cmd --add-port=3306/tcp --permanent
systemctl restart firewalld

配置root密码

mysqladmin -u root password 'mysql'mysql -uroot -p


创建一个测试用的数据库和表

MariaDB [(none)]> create database mydb;Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> use mydb
Database changed
MariaDB [mydb]> create table student(id int not null auto_increment,name varchar(10), primary key(id));Query OK, 0 rows affected (0.04 sec)MariaDB [mydb]> insert into student(name) values('Jay'),('Bob'),('Alex');Query OK, 3 rows affected (0.00 sec)Records: 3  Duplicates: 0  Warnings: 0

MariaDB [mydb]> select * from student;+----+------+| id | name |+----+------+|  1 | Jay  ||  2 | Bob  ||  3 | Alex |+----+------+
3 rows in set (0.00 sec)


创建一个远程访问的账户

MariaDB [(none)]> create user yli@10.2.100.60;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> set password for yli@'10.2.100.60'=password('yli');Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> grant all privileges on mydb.* to yli@10.2.100.60;Query OK, 0 rows affected (0.00 sec)


然后安装一个图形界面的工具Navicat,绑定数据库

MySQL数据库之Python 学习笔记 - 操作MySQL

MySQL数据库之Python 学习笔记 - 操作MySQL

这样一个基本的测试环境就搭建好了。


现在来看看pymysql的使用。


在我的客户端安装一下pymysql的模块

C:\WINDOWS\system32>pip install pymysql
Collecting pymysql
  Downloading PyMySQL-0.7.9-py3-none-any.whl (78kB)
    100% |################################| 81kB 610kB/sInstalling collected packages: pymysql
Successfully installed pymysql-0.7.9


Python源码演示


查询

#!/usr/bin/env python# -*- coding:utf-8 -*-import pymysql#打开数据库连接conn = pymysql.connect(host='sydnagios', port=3306, user='yli', passwd='yli', db='mydb')#创建一个游标对象cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)#SQL查询cursor.execute(""select * from student"")# 获取第一行数据# row_1 = cursor.fetchone()# print(row_1)# 获取前n行数据# row_2 = cursor.fetchmany(3)# 获取所有数据row_3 = cursor.fetchall()print(row_3)#scroll可以使用相对位置或者绝对位置,这里相对位置(末尾)向上移动2行cursor.scroll(-2,mode='relative')row_3 = cursor.fetchall()print(row_3)#提交,不然无法保存新的数据conn.commit()#关闭游标cursor.close()#关闭连接conn.close()-----------[{'id': 1, 'name': 'Jay'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Alex'}][{'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Alex'}]


修改

#!/usr/bin/env python# -*- coding:utf-8 -*-import pymysql
conn = pymysql.connect(host='sydnagios', port=3306, user='yli', passwd='yli', db='mydb')cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)cursor.execute(""Update student set name='BoB' where id=2"")cursor.execute(""select * from student"")row_3 = cursor.fetchall()print(row_3)conn.commit()cursor.close()conn.close()----------[{'id': 1, 'name': 'Chris'}, {'id': 2, 'name': 'BoB'}, {'id': 3, 'name': 'Alex'}]


删除

#!/usr/bin/env python# -*- coding:utf-8 -*-import pymysql
conn = pymysql.connect(host='sydnagios', port=3306, user='yli', passwd='yli', db='mydb')cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)cursor.execute(""delete from student where id=2"")cursor.execute(""select * from student"")row_3 = cursor.fetchall()print(row_3)conn.commit()cursor.close()conn.close()----------[{'id': 1, 'name': 'Chris'}, {'id': 2, 'name': 'BoB'}, {'id': 3, 'name': 'Alex'}]


添加

#!/usr/bin/env python# -*- coding:utf-8 -*-import pymysql
conn = pymysql.connect(host='sydnagios', port=3306, user='yli', passwd='yli', db='mydb')cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)cursor.execute(""insert into student(name) value ('ZhangSan'),('LiSi')"")cursor.execute(""select * from student"")row_3 = cursor.fetchall()print(row_3)conn.commit()cursor.close()conn.close()----------[{'name': 'Chris', 'id': 1}, {'name': 'Alex', 'id': 3}, {'name': 'ZhangSan', 'id': 4}, {'name': 'LiSi', 'id': 5}]

本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程