• 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

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

     
  • 相关阅读:
    [BZOJ4480] JSOI2013 快乐的jyy
    [BZOJ4755] JSOI2016 扭动的回文串
    [BZOJ4754] JSOI2016 独特的树叶
    [BZOJ5179] JSOI2011 任务调度
    [BZOJ4320] SHOI2006 Homework
    [AT2300] Snuke Line
    [洛谷P3974] TJOI2015 组合数学
    [CF331E] Biologist
    [洛谷P4249] WC2007 剪刀石头布
    【BZOJ4548】小奇的糖果
  • 原文地址:https://www.cnblogs.com/lixy-88428977/p/9548833.html
Copyright © 2020-2023  润新知