您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
今天小编给大家分享一下PyMySQL怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
我们在使用MySQL的时候,可以在MySQL的客户终端来操作数据库中的表,同时,也可以使用navicat等可视化的工具来操作数据表。但是,这只是操作个别数据,如果我们想要插入10万条数据,那肯定就不能这么做了。我们可以通过程序写一个循环来自动插入,因此,PyMySQL就是使用python语言来直接操作数据库的一个接口。
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)
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("结束!")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("结束!")4 删除数据:
# 需求:
# 增加数据 刘德华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("结束!")以上就是“PyMySQL怎么使用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。