Python操作MySql --Python3
Python版本:v3.7
模块:pymysql
1、连接数据库
connectDB.py:
- # coding:utf-8
import pymysql- host = 'localhost' # 主机
username = 'root' # 用户名
pwd = 'nxl123' # 密码
dbName = 'testdb' # 数据库名
# 打开数据库连接
db = pymysql.connect(host, username, pwd, dbName)
# 通过cursor方法获取操作游标
cursor = db.cursor()
# sql语句
sql = 'SELECT VERSION()'
# 执行sql语句
cursor.execute(sql)
# 使用fetchone方法获取一个查询结果集
data = cursor.fetchone()
# 输出结果集
print('db version:%s' % data)
# 关闭数据库连接
db.close()
2、创建表
createTables.py:
- # coding:utf-8
import pymysql- host = 'localhost' # 主机
username = 'root' # 用户名
pwd = 'nxl123' # 密码
dbName = 'testdb' # 数据库名
# 打开数据库连接
db = pymysql.connect(host, username, pwd, dbName)
# 通过cursor方法获取操作游标
cursor = db.cursor()
# 执行sql语句,如果存在stu和sex表,就将其删除
cursor.execute('DROP TABLE IF EXISTS stu')
cursor.execute('DROP TABLE IF EXISTS sex')
# sql语句,创建stu表
sql = '''CREATE TABLE stu(
stuId SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
stuNo VARCHAR(20) NOT NULL UNIQUE KEY,
stuName VARCHAR(10) NOT NULL,
age SMALLINT UNSIGNED NOT NULL,
sexId SMALLINT UNSIGNED NOT NULL)'''
sql2 = '''CREATE TABLE sex(
id SMALLINT UNSIGNED PRIMARY KEY,
sex ENUM('男','女') NOT NULL)'''
# 执行sql、sql2语句
cursor.execute(sql)
cursor.execute(sql2)
# 关闭数据库连接
db.close()
3、插入数据
insertData.py:
- # coding:utf-8
import pymysql- host = 'localhost' # 主机
username = 'root' # 用户名
pwd = 'nxl123' # 密码
dbName = 'testdb' # 数据库名
db = pymysql.connect(host, username, pwd, dbName)
# 通过cursor方法获取操作游标
cursor = db.cursor()
# sql语句
# sql = '''INSERT stu(stuNo,stuName,age,sexId) VALUES('2015011070','Thanlon',22,1) '''
# sql = '''INSERT stu(stuNo,stuName,age,sexId) VALUES('2015011071','Maria',20,2) '''
# sql = '''INSERT sex(id,sex) VALUES(1,'男')'''
sql = '''INSERT sex(id,sex) VALUES(2,'女')'''
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# 发生错误时回滚
db.rollback()
# 关闭数据库连接
db.close()
stu 表:
sex 表:
4、删除数据
deleteData.py:
- # coding:utf-8
import pymysql- host = 'localhost' # 主机
username = 'root' # 用户名
pwd = 'nxl123' # 密码
dbName = 'testdb' # 数据库名
# 打开数据库连接
db = pymysql.connect('localhost', username, pwd, dbName)
# 通过cursor方法获取操作游标
cursor = db.cursor()
sql = "DELETE FROM stu WHERE age>'%d'" % (20)
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# 语句产生异常时打印提示信息
print('更新数据时出现异常!')
finally:
# 关闭数据库连接
db.close()
stu表:(删除数据后的stu表)
5、查询数据
selectData.py:
- # coding:utf-8
import pymysql- host = 'localhost' # 主机
username = 'root' # 用户名
pwd = 'nxl123' # 密码
dbName = 'testdb' # 数据库名
# 打开数据库连接
db = pymysql.connect(host, username, pwd, dbName)
# 通过cursor方法获取操作游标
cursor = db.cursor()
# sql = '''SELECT *FROM stu'''
sql = '''SELECT stuID,stuNo,stuName,age,x.sex FROM sex AS x INNER JOIN stu AS s ON s.sexId = x.id'''
try:
# 执行sql语句
cursor.execute(sql)
# results接收全部的返回结果行
results = cursor.fetchall()
# print(results)
# 返回执行execute方法后影响的行数
results_count = cursor.rowcount
# 打印输出影响的行数
print('execute()方法执行后影响的行数:%d行' % results_count)
# 遍历结果集
for row in results:
stuID = row[0]
stuNo = row[1]
stuName = row[2]
age = row[3]
sex = row[4]
# 打印查询结果
print(stuID, stuNo, stuName, age, sex)
except:
print('获取数据出现异常!')
finally:
# 关闭数据库连接
db.close()
6、修改数据
updateData.py:
- # coding:utf-8
import pymysql- host = 'localhost' # 主机
username = 'root' # 用户名
pwd = 'nxl123' # 密码
dbName = 'testdb' # 数据库名
# 打开数据库连接
db = pymysql.connect(host, username, pwd, dbName)
# 通过cursor方法获取操作游标
cursor = db.cursor()
- # 将性别为男的学生年龄加1
- sql = '''UPDATE stu SET age=age+1 WHERE sexId=1'''
try:
# 执行语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
print('更新数据时出现异常!')
finally:
# 关闭数据库连接
db.close()
执行语句前stu表信息:
执行语句后stu表信息:
Python操作MySql --Python3的更多相关文章
- Python开发【第十一篇】:Python操作MySQL
本篇对于Python操作MySQL主要使用两种方式: 1.原生模块pymsql. 2.ORM框架SQLAchemy. pymsql pymsql是Python中操作MySQL的模块,其使用方法和MyS ...
- Python 操作 MySQL 的5种方式(转)
Python 操作 MySQL 的5种方式 不管你是做数据分析,还是网络爬虫,Web 开发.亦或是机器学习,你都离不开要和数据库打交道,而 MySQL 又是最流行的一种数据库,这篇文章介绍 Pytho ...
- Python使用MySQLConnector/Python操作MySQL、MariaDB数据库
使用MySQL Connector/Python操作MySQL.MariaDB数据库 by:授客 QQ:1033553122 因目前MySQLdb并不支持python3.x,而MySQL官方已经提 ...
- 转载 Python 操作 MySQL 的正确姿势 - 琉璃块
Python 操作 MySQL 的正确姿势 收录待用,修改转载已取得腾讯云授权 作者 |邵建永 编辑 | 顾乡 使用Python进行MySQL的库主要有三个,Python-MySQL(更熟悉的名字可能 ...
- 笔记-python操作mysql
笔记-python操作mysql 1. 开始 1.1. 环境准备-mysql create database db_python; use db_python; create tabl ...
- Python操作MySQL[转]
本篇对于Python操作MySQL主要使用两种方式: 1.原生模块pymsql. 2.ORM框架SQLAchemy. pymsql pymsql是Python中操作MySQL的模块,其使用方法和MyS ...
- python接口自动化(三十八)-python操作mysql数据库(详解)
简介 现在的招聘要求对QA人员的要求越来越高,测试的一些基础知识就不必说了,来说测试知识以外的,会不会一门或者多门开发与语言,能不能读懂代码,会不会Linux,会不会搭建测试系统,会不会常用的数据库, ...
- Python操作MySQL数据库(步骤教程)
我们经常需要将大量数据保存起来以备后续使用,数据库是一个很好的解决方案.在众多数据库中,MySQL数据库算是入门比较简单.语法比较简单,同时也比较实用的一个.在这篇博客中,将以MySQL数据库为例,介 ...
- Python 操作 MySQL 的5种方式
不管你是做数据分析,还是网络爬虫,Web 开发.亦或是机器学习,你都离不开要和数据库打交道,而 MySQL 又是最流行的一种数据库,这篇文章介绍 Python 操作 MySQL 的5种方式,你可以在实 ...
随机推荐
- topcoder srm 445 div1
problem1 link 这个的结论是只需要考虑坐标是整数或者是整数.5,比如(2.5,3),(4,3.5),(1.5,4.5)这样的时候.这个详细证明起来应该挺麻烦的.这里有一些讨论. probl ...
- html的进一步了解(更新中···)
(接上一次) 属性:表示事物的一些特征 属性又可分为两种: 标签属性和样式属性 两者的区别: 位置不一样 样式属性写在style中 标签属性写在标签内 写法不一样 样式属性是属性:属性值 标签属性是属 ...
- 2015,3,10 2(南阳理工ACM)
描述有一个整型偶数n(2<= n <=10000),你要做的是:先把1到n中的所有奇数从小到大输出,再把所有的偶数从小到大输出. 输入 第一行有一个整数i(2<=i<30) ...
- linux命令之crontab定时执行任务【转】
本文转载自:https://www.cnblogs.com/coffy/p/5608095.html 一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护 ...
- git如何修改用户名和邮箱名?
答: 使用git config --global --edit即可进行修改
- openwrt编译e2fsprogs-1.43时报错misc/create_inode.c:399:18: error: conflicting types for 'copy_file_range'
1. 详细报错信息 misc/create_inode.c:399:18: error: conflicting types for 'copy_file_range' static errcode_ ...
- noip模拟【ping】
70:很容易想到的是枚举每一个可能的答案来判断是否可行,取最优即可,贪心选择. 100:满足题目条件的这个距离是满足单调性的.如果x不行,那么大于x的距离都不行,二分答案. 学会运用二分,by ws_ ...
- SSM项目开发中的实体定义以及MySQL表格设计
话不多说,下面表格是项目开发中用到的实体集以及表格Name 实体创建 表格创建 Area 区域 Area 实体 areaId areaName priority createTime last ...
- Tutorial: Generate BBox or Rectangle to locate the target obejct
Tutorial: Generate BBox or Rectangle to locate the target obejct clc;close all;clear all; Img=imread ...
- template render in javascript
art-template for github 中文官方文档