python基础_mysql建表、编辑、删除、查询、更新
1.建一张学生表 包含(id,name,age,sex)
2.增加四条数据
3.查询表中sex为男的数据
4.删除id =3的数据,
5.将sex为女的,修改为男 create:
CREATE TABLE data_test(
id INT unique,
name VARCHAR(50),
age INT,
sex enum('男','女')
)


insert:
insert into data_test(id, name, age, sex)
VALUES(1, '李牧', 18, '男'),
(2, '栗子', 20, '女'),
(3, '测试', 26, '男'),
(4, '尕娃', 30, '女')

select:
select name from data_test where sex='男'

delete:
delete from data_test where id=3

updata:
update data_test set sex='男' where sex='女'

完整代码如下:
import pymysql
class Mysql():
def create(self):
try:
sql_creat = """CREATE TABLE data_test(
id INT unique,
name VARCHAR(50),
age INT,
sex enum('男','女')
)"""
cursor.execute(sql_creat)
print("建表成功")
except UserWarning:
print("建表失败")
def insert(self):
try:
sql_insert = """insert into data_test(id, name, age, sex) VALUES(1, '李牧', 18, '男'),(2, '栗子', 20, '女'),(3, '测试', 26, '男'),(4, '尕娃', 30, '女')"""
cursor.execute(sql_insert)
print("插入数据成功") except UserWarning:
print("插入数据失败")
def select(self):
try:
sql_select = """select name from data_test where sex='男'"""
cursor.execute(sql_select)
print("查询数据成功")
except UserWarning:
print("插入数据失败")
def delete(self):
try:
sql_delete = """delete from data_test where id=3"""
cursor.execute(sql_delete)
print("数据删除成功")
except UserWarning:
print("数据删除失败")
def updata(self):
try:
sql_update = """update data_test set sex='男' where sex='女'"""
cursor.execute(sql_update)
print("数据更新成功")
except UserWarning:
print("数据更新失败")
def delete_table(self):
sql_delete_table = """drop table data_test"""
cursor.execute(sql_delete_table)
print("清洗数据:删除数据表") if __name__ == '__main__':
con = pymysql.connect("localhost", "root", "", "test04", charset='utf8')
cursor = con.cursor()
# 实例化
test = Mysql()
# 建一张学生表 包含(id,name,age,sex)
test.create()
# 增加四条数据
test.insert()
con.commit() # 提交
# 查询表中sex为男的数据
test.select()
print(cursor.fetchall())
# 删除id =3的数据
test.delete()
con.commit() # 提交
# 将sex为女的,修改为男
test.updata()
con.commit() # 提交
test.delete_table()
cursor.close()
作者:含笑半步颠√
博客链接:https://www.cnblogs.com/lixy-88428977
声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。
python基础_mysql建表、编辑、删除、查询、更新的更多相关文章
- [SQL基础教程] 1-5 表的删除和更新
[SQL基础教程] 1-5 表的删除和更新 表的删除 语法 DROP TABLE <表名>; 法则 1-12 删除的表无法恢复 表定义的更新 语法 ALTER TABLE<表名> ...
- 通过python给mysql建表
一.python连接mysql from sqlalchemy import create_engine # 数据库数据 HOSTNAME = '127.0.0.1' # linux本地 PORT = ...
- mysql 个人博客应用的建表和相关查询
一.建表 用户表tb_user create table if not exists tb_user( user_id int auto_increment, ) not null, user_pas ...
- SqlServer 循环建表、删除表、更新表
常用于分库分表 1.批量删除 declare @outter int declare @inner int ) ) ) begin set @tablePrefix='BankPayOrder_'+c ...
- MySQL基础2-创建表和主键约束
1.创建表 在操作数据表之前,应该使用"USE 数据库名"指定操作是在哪个数据库中进行 主键约束(唯一标识) ****非空*** ****唯一*** ****被引用****(学习外 ...
- 小贝_mysql建表以及列属性
mysql建表以及列属性 简要: 一.建表原则 二.具体的列属性说明 一.建表原则 建表: 事实上就是声明列的过程,数据终于是以文件的形式放在硬盘(内存) 列: 不同的列类型占的空间不一样. 选列的原 ...
- hibernate的基础学习--多表关联数据查询
Hibernate共提供4种多表关联数据查询方式 OID数据查询+OGN数据查询方式 HQL数据查询方式 QBC数据查询方式 本地SQL查询方式(hibernate很少用) 1.OID数据查询+OGN ...
- python基础学习1-列表使用
python 列表相关操作方法 namelist = ['a','b','c','d','1','2','3','4'] namelist1 = ['a','b','c','d','1','2','3 ...
- python 基础知识3-列表元祖
1.列表增. # append 追加,给列表的最后面追加一个元素 li = ['alex','wufa','太白','大白'] li.append('教师') li.append(1) print(l ...
随机推荐
- gerrit配置跳过审核直接push到gitlab
项目中有存放项目相关的文档,这些项目需要配置跳过审核再提交的操作.现在需要给某些组配置不审核直接提交的权限 方法: 使用管理员账号,到 projects -> access 页面下配置 refe ...
- PKUWC2019 Round 2 没去祭
因为今年有两场 PKUWC,所以叫 PKUWC2019 Round 2. 因为一些沙雕原因,今年去不了了. Day 0 一如既往,没有集训就去上数学课,今天讲几何变换,一如既往的只会说"少女 ...
- js中forEach,for in,for of循环的用法详解
一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...
- Python3菜鸟教程笔记
多行语句 同一行显示多条语句 Print 输出
- P5597 【XR-4】复读
枚举终点u,把路径(1,u)压起来(不考虑u的子树),并起来之后暴力
- HAProxy+Keepalived高可用负载均衡
一 基础准备 1.1 部署环境及说明 系统OS:CentOS 6.8 64位 HAProxy软件:HA-Proxy version 1.5.18 Keepalived软件:keepalived-1.3 ...
- Codeforces 839D Winter is here - 暴力 - 容斥原理
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n s ...
- go 练习:HTTP 处理
这篇文章只是联系go指南时的笔记吧. package main import ( "fmt" "log" "net/http" ) type ...
- java通过请求对象获取ip地址、获取ip地址
/** * 获取登录ip */ public String getIp(){ HttpServletRequest request = this.getRequest(); String ip = & ...
- CS224n学习笔记(二)
Global Vectors for Word Representation (GloVe) GloVe 模型包含一个训练在单词-单词的共同出现次数上的加权的最小二乘模型. 什么是Co-occurre ...