zoukankan      html  css  js  c++  java
  • 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", "123456", "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

    声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。

     
  • 相关阅读:
    SAP Cloud for Customer的Account Team里的role如何配置
    SAP标准培训课程C4C10学习笔记(二)第二单元
    SAP标准培训课程C4C10学习笔记(一)第一单元
    如何找到SAP Cloud for Customer标准培训和认证方面的信息
    我用ABAP做过的那些无聊的事情
    有道云笔记不需要通过开通会员的方式来去除广告显示
    微软OneDrive使用体验
    自己开发的在线视频下载工具,基于Java多线程
    CloudFoundry命令行和Kubernetes命令行的Restful API消费方式
    利用装饰器计算函数运行的时间
  • 原文地址:https://www.cnblogs.com/lixy-88428977/p/9548833.html
Copyright © 2011-2022 走看看