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建表、编辑、删除、查询、更新的更多相关文章

  1. [SQL基础教程] 1-5 表的删除和更新

    [SQL基础教程] 1-5 表的删除和更新 表的删除 语法 DROP TABLE <表名>; 法则 1-12 删除的表无法恢复 表定义的更新 语法 ALTER TABLE<表名> ...

  2. 通过python给mysql建表

    一.python连接mysql from sqlalchemy import create_engine # 数据库数据 HOSTNAME = '127.0.0.1' # linux本地 PORT = ...

  3. mysql 个人博客应用的建表和相关查询

    一.建表 用户表tb_user create table if not exists tb_user( user_id int auto_increment, ) not null, user_pas ...

  4. SqlServer 循环建表、删除表、更新表

    常用于分库分表 1.批量删除 declare @outter int declare @inner int ) ) ) begin set @tablePrefix='BankPayOrder_'+c ...

  5. MySQL基础2-创建表和主键约束

    1.创建表 在操作数据表之前,应该使用"USE 数据库名"指定操作是在哪个数据库中进行 主键约束(唯一标识) ****非空*** ****唯一*** ****被引用****(学习外 ...

  6. 小贝_mysql建表以及列属性

    mysql建表以及列属性 简要: 一.建表原则 二.具体的列属性说明 一.建表原则 建表: 事实上就是声明列的过程,数据终于是以文件的形式放在硬盘(内存) 列: 不同的列类型占的空间不一样. 选列的原 ...

  7. hibernate的基础学习--多表关联数据查询

    Hibernate共提供4种多表关联数据查询方式 OID数据查询+OGN数据查询方式 HQL数据查询方式 QBC数据查询方式 本地SQL查询方式(hibernate很少用) 1.OID数据查询+OGN ...

  8. python基础学习1-列表使用

    python 列表相关操作方法 namelist = ['a','b','c','d','1','2','3','4'] namelist1 = ['a','b','c','d','1','2','3 ...

  9. python 基础知识3-列表元祖

    1.列表增. # append 追加,给列表的最后面追加一个元素 li = ['alex','wufa','太白','大白'] li.append('教师') li.append(1) print(l ...

随机推荐

  1. 桥接:JS调用安卓方法报错Uncaught Error: Error calling method on NPObject

    说一说自己粗心踩到的一个不算坑的坑: 项目是安卓webview嵌入SPA单页应用页面,涉及到JS调用原生安卓方法,但就是在调用安卓方法时死活一直报错xxx  NPObject一堆错误.写了一个测试页面 ...

  2. GoCN每日新闻(2019-10-17)

    GoCN每日新闻(2019-10-17) 通过go module管理go tool https://marcofranssen.nl/manage-go-tools-via-go-modules/ 使 ...

  3. Comparison of SIFT Encoded and Deep Learning Features for the Classification and Detection of Esca Disease in Bordeaux Vineyards(分类MobileNet,目标检测 RetinaNet)

    识别葡萄的一种虫害,比较了传统SIFT和深度学习分类,最后还做了目标检测 分类用的 MobileNet,目标检测 RetinaNet MobileNet 是将传统深度可分离卷积分成了两步,深度卷积和逐 ...

  4. 第12组 Beta测试(5/5)

    Header 队名:To Be Done 组长博客 作业博客 团队项目进行情况 燃尽图(组内共享) 展示Git当日代码/文档签入记录(组内共享) 注: 由于GitHub的免费范围内对多人开发存在较多限 ...

  5. springboot带有进度条的上传

    一.说明 最近要做文件上传,在网上找了很久都没有一个全面的示例,特此记录下来分享给大家. 1.文件上传接口可按照springboot默认实现,也可用commons-fileupload组件,本示例使用 ...

  6. POP IM 产品分析报告

    一.   体验环境 产品名称:POP IM 软件版本:v2.4.0 手机系统:一加5T Android 9 体验时间:2019.10.22-2019.10.31 二.   产品简介 1.   产品定位 ...

  7. Python selenium PO By.XPATH定位元素报错

    Python selenium PO  By.XPATH定位元素报错 如下代码经常报错: # 首页的“新建投放计划”按钮 new_ads_plan = (By.XPATH, "//*[tex ...

  8. remaining connection slots are reserved for non-replication superuser connections

    使用客户端工具连接pg,连接失败,报错以下错误: FATAL: remaining connection slots are reserved for non-replication superuse ...

  9. 013 Enhancer创建动态代理

    在上一章使用了Enhancer,没有仔细说明代理的问题,在这一章进行详细的说明. 原本想使用cf包的,后来发现使用framework包下的包也没有问题,程序中就一直使用framework中的类. 一: ...

  10. elementui---for循环需要添加KEY

    在用VUE和elementui开发项目的时候,在开启 es-lient 的时候,如果for循环没有添加 key ,会报语法上的错误. genderSelect:[ {value:0,label:'女' ...