python操作数据库mysql

参考链接:https://blog.csdn.net/caixiangting/article/details/131425049?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170138984316800185840590%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=170138984316800185840590&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1~rank_v31_ecpm-2-131425049-null-null.nonecase&utm_term=%E5%A6%82%E4%BD%95%E5%B0%86%E6%95%B0%E6%8D%AE%E5%BA%93mysql%E7%9A%84%E4%BF%A1%E6%81%AF%E6%98%BE%E7%A4%BA%E5%9C%A8%E7%BD%91%E9%A1%B5%E4%B8%8Apython&spm=1018.2226.3001.4450
首先确保自己安装了mysql,在mysql下新建数据库test,在test下新建books的表格
我的表格如下:
mysql> select*from books;
±-----±---------±------±--------+
| name | author | price | class |
±-----±---------±------±--------+
| java | zhangsan | 40.0 | jiaocai |
| c++ | lisi | 30.0 | jiaocai |
±-----±---------±------±--------+
代码整合:

import pymysql

# 定义一个函数,用来创建连接数据库
con = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        database='mysql',
        charset='utf8',
        user='root',
        password='****'
    )

def select():
    try:
        with con.cursor() as cursor:
        # 一、查询
            sql = "select * from test.books;"
        # 执行SQL语句
        # 执行后的结果都保存在cursor中
            cursor.execute(sql)

        # 1-从cursor中获取全部数据用fetchall
        # datas = cursor.fetchall()
        # print("获取的数据:\n",datas)

        # 2-从cursor中获取一条数据用fetchall
        # data = cursor.fetchone()
        # print("获取的数据:\n",data)

        # 3-想要从cursor中获取几条数据用fetchmany
            datas = cursor.fetchmany(1)
            print("获取的数据:\n", datas)
    except Exception as e:
            print("数据库异常:\n", e)
    finally:
        # 不管成功还是失败,都要关闭数据库连接
            con.close()

def insert():
    try:
        with con.cursor() as cursor:
        # 二、插入
        # 1-插入一条数据
            sql = "insert into test.books(name, author,price,class)value('android','xx',30,'123');"
        # 执行SQL语句
            cursor.execute(sql)
        # 执行玩SQL语句要提交
            con.commit()
            print("提交成功")

    except Exception as e:
    # 如果执行失败要回滚
            con.rollback()
            print("数据库异常:\n", e)
    finally:
            con.close()
# 不管成功还是失败,都要关闭数据库连接

def update():

    try:
        with con.cursor() as cursor:
            # 三、更新
            sql = f"update test.books set author='yyy' where author='xx';"
            # 执行SQL语句
            cursor.execute(sql)
            # 执行完SQL语句要提交
            con.commit()
            print("提交成功")

    except Exception as e:
        # 如果执行失败要回滚
            con.rollback()
            print("数据库异常:\n", e)
    finally:
        # 不管成功还是失败,都要关闭数据库连接
            con.close()

def dell():
    try:
        with con.cursor() as cursor:
     
            sql = "delete from test.books where author like 'yyy';"
            # 执行SQL语句
            cursor.execute(sql)
            # 执行完SQL语句要提交
            con.commit()
            print("提交成功")
    except Exception as e:
        # 如果执行失败要回滚
        	con.rollback()
        	print("数据库异常:\n", e)
    finally:
        # 不管成功还是失败,都要关闭数据库连接
        	con.close()

需要用哪种功能就调用哪个函数

你可能感兴趣的:(python,数据库,python,mysql)