使用PyMySQL怎么实现增删查改操作

发布时间:2021-05-13 16:18:10 作者:Leah
来源:亿速云 阅读:119

使用PyMySQL怎么实现增删查改操作?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1、PyMySQL的使用步骤:

使用PyMySQL怎么实现增删查改操作

使用PyMySQL怎么实现增删查改操作

使用PyMySQL怎么实现增删查改操作

使用PyMySQL怎么实现增删查改操作

2、案例:

2.1 查询数据库中的表的信息:

 # 需求:查询数据库person中info表的信息

 # 1.导包
import pymysql

try:
     # 2.连接MySQL数据库的服务
    connc = pymysql.Connect(
                    user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
                    password="4412",
                    host='127.0.0.1',  # mysql服务端的IP,默认是127.0.0.1/localhost,或者写真实的ip
                    database='person',
                    port=3306,
                    charset="utf8")
     # 3.创建游标对象
    cur = connc.cursor()
     # 4.编写SQL语句
    sql = 'select * from info;'
     # 5.使用游标对象调用SQL
    cur.execute(sql)
     # 6.获取查询的结果
    result= cur.fetchall()
    print(result)
    # 7.关闭游标对象
    cur.close()
    # 8.关闭连接
    connc.close()

except Exception as e:
    print(e)

运行结果:

使用PyMySQL怎么实现增删查改操作

2.2 增加数据:

大部分的步骤都和前面一样,直接在程序中注释看:

# 需求:
# 增加数据 刘德华56 男 数据 到 数据库person--的info表中
# 修改数据 小王 的名字为 小王吧 到 数据库person--的info表中
# 删除数据 张三      数据库person--的info表中

# 1.导包
import pymysql

# 2.连接MySQL服务
connc = pymysql.Connect(
    user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
    password="4412",
    host='127.0.0.1',  # mysql服务端的IP,默认是127.0.0.1/localhost,或者写真实的ip
    database='person',
    port=3306,
    charset="utf8")

# 3.创建游标对象
cur = connc.cursor()

try:
    # 4.编写、增加、删除的SQL语句    
    # 增加数据 刘德华 56 男
    sql = 'insert into info values(%s, %s, %s, %s)'
    add_data = [0,"刘德华", 56, "男"]
    
    # 5.使用游标对象执行SQL语句
    cur.execute(sql, add_data)
    
    # 6.提交操作
    connc.commit()
    
except Exception as e:
    print(e)
    # 操作失败,数据回滚
    connc.rollback()
    
finally:
    # 7.关闭游标对象
    cur.close()
    
    # 8.关闭连接
    connc.close()

print("结束!")

运行之后,看看person数据库中 表info 的数据,确实增加成功了:

使用PyMySQL怎么实现增删查改操作

2.3 修改数据:

# 需求:
# 增加数据 刘德华56 男 数据 到 数据库person--的info表中
# 修改数据 小王 的名字为 小王吧 到 数据库person--的info表中
# 删除数据 张三      数据库person--的info表中

# 1.导包
import pymysql

# 2.连接MySQL服务
connc = pymysql.Connect(
    user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
    password="4412",
    host='127.0.0.1',  # mysql服务端的IP,默认是127.0.0.1/localhost,或者写真实的ip
    database='person',
    port=3306,
    charset="utf8")

# 3.创建游标对象
cur = connc.cursor()

try:
    # 4.编写、增加、删除的SQL语句
    # 修改数据 李四 的名字为 李四的爸爸
    sql = 'update info set name=%s where name="李四"'
    update_data = ["李四的爸爸"]
        
    # 5.使用游标对象执行SQL语句
    cur.execute(sql, update_data)
    
    # 6.提交操作
    connc.commit()
    
except Exception as e:
    print(e)
    #  操作失败,数据回滚
    connc.rollback()
    
finally:
    # 7.关闭游标对象
    cur.close()
    
    # 8.关闭连接
    connc.close()

print("结束!")

运行之后,看看person数据库中 表info 的数据,确实修改成功了:

使用PyMySQL怎么实现增删查改操作

2.3 删除数据:

# 需求:
# 增加数据 刘德华56 男 数据 到 数据库person--的info表中
# 修改数据 小王 的名字为 小王吧 到 数据库person--的info表中
# 删除数据 张三      数据库person--的info表中

# 1.导包
import pymysql

# 2.连接MySQL服务
connc = pymysql.Connect(
    user="root",  # The first four arguments is based on DB-API 2.0 recommendation.
    password="4412",
    host='127.0.0.1',  # mysql服务端的IP,默认是127.0.0.1/localhost,或者写真实的ip
    database='person',
    port=3306,
    charset="utf8")

# 3.创建游标对象
cur = connc.cursor()

try:
    # 4.编写、增加、删除的SQL语句
    # 修改数据 李四 的名字为 李四的爸爸
    sql = 'update info set name=%s where name="李四"'
    update_data = ["李四的爸爸"]
        
    # 5.使用游标对象执行SQL语句
    cur.execute(sql, update_data)
    
    # 6.提交操作
    connc.commit()
    
except Exception as e:
    print(e)
    #  操作失败,数据回滚
    connc.rollback()
    
finally:
    # 7.关闭游标对象
    cur.close()
    
    # 8.关闭连接
    connc.close()

print("结束!")

运行之后,看看person数据库中 表info 的数据,确实删除成功了:

使用PyMySQL怎么实现增删查改操作

关于使用PyMySQL怎么实现增删查改操作问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. 使用pymysql的方法
  2. 使用JavaScript 怎么对MongoDB进行增删查改操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pymysql

上一篇:BCELoss和BCEWithLogitsLoss怎么在Pytorch中使用

下一篇:如何在Android中实现一个音乐播放器

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》