Python操作MySql --Python3
2024-10-18 02:27:04
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表信息:
最新文章
- 借助亚马逊S3和RapidMiner将机器学习应用到文本挖掘
- MFC--响应鼠标和键盘操作
- ipv4理论知识1-ipv4介绍,ipv4记法,地址段个数算法
- html,css命名规范 (转)
- ansible加密命令
- cocos2d-x之Vector与map
- shell 随机从文件中抽取若干行
- nyoj 93 汉诺塔(三)
- 406. Queue Reconstruction by Height
- 一个给力的html5 画多边形的例子
- crawler_x-requested-with 请求头
- 如何成为一名hacker?
- C#实现阿拉伯数字(小写金额)到大写中文(大写金额)的转换
- 二十一、Linux 进程与信号---进程查看和进程状态、进程调度和进程状态变化、进程标识
- RepBaseRepeatMaskerEdition下载 | RepeatMasker
- 多点搜的bfs
- python 基本数据类型 之 字符串
- 总结Linux 下Redis 操作常用命令(转)
- (Power Strings)sdutoj2475
- Android中利用ant进行多渠道循环批量打包
热门文章
- Codeforces Round #427 (Div. 2) Problem A Key races (Codeforces 835 A)
- centos7 install fastdfs nginx
- day24,25组合 封装 多态
- uniGUI试用笔记(九)
- 修改userdata的分区大小【转】
- HDU - 1849 Rabbit and Grass 【Nim博弈】
- 找质数|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)
- 【学习】Hall’s Marriage Theorem
- 【Mybatis】-- Mapper动态代理开发注意事项
- .NET控件集ComponentOne 2018V3发布:新增图表动画及迷你图