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. 洛谷 P1629 邮递员送信 题解

    P1629 邮递员送信 题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要 ...

  2. Processing设计Android APP(1) - 安装

    1.安装环境: A. Android Studio B. Processing 3.4 (64bit) 首先,直接安装Android Studio,我这里版本是3.2.1. 然后,新建一个Sample ...

  3. string拼接时去掉最后一个逗号

     str.replace(str.length() - 1, str.length(), "");

  4. sublime text 3插件改造之添加从模版新增文件到指定目录

    简介:以前使用ST2里面的Sublime NFFT插件比较顺手,最近安装了ST3,但是Sublime NFFT插件不支持ST3,就下载了SublimeTmpl从模版新建文件插件.在使用时,习惯在侧边栏 ...

  5. Git常用命令及常见报错:You have not concluded your merge (MERGE_HEAD exists)、清理无效的远程追踪分支

    一.常用命令 切换到master分支:git checkout master 查看已有本地及远程分支:git branch -a(先git pull拉下全部数据) 查看远程分支:git branch ...

  6. spring boot eclipse 远程调试

    <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...

  7. Jmeter命令行运行配置环境变量

    Jmeter命令行运行配置环境变量 在打开jmeter GUI界面时会弹出cmd命令窗口提示:压测时不要用GUI,要用命令行 在cmd命令行里面运行jmeter的话,需要配置jmeter环境变量,下面 ...

  8. dubbo架构角色

    角色 Dubbo有5个参与者:其中Monitor.Registry不是必须的 Provider 暴露服务的服务提供方 Consumer 调用远程服务的服务消费方(负载均衡) Registry 服务注册 ...

  9. oracle update from多表性能优化一例

    这几天测试java内存数据库,和oracle比较时发下一个update from语句很慢,如下: update business_new set fare1_balance_ratio = (sele ...

  10. 包含MANIFEST.MF的jar可执行应用指定classpath及spring boot应用增量升级打包实现

    对于不包含MANIFEST.MF,或jar包中的MANIFEST.MF未指定MainClass的jar,可以通过java命令行选项-classpath指定classpath.但是如果是包含MainCl ...