侧边栏壁纸
博主昵称
梦之中小俊

以前高中时,羡慕大学考试只要及格就行;现在大学了,怀念高中考试及不及格都行??

使用python对mysql数据库进行添加数据的操作

梦之中小俊
2023-03-18 / 0 评论 / 360 阅读 / 推送成功!
使用python连接mysql进行添加数据的操作

使用的是python3.6+pymysql

1、导入pymysql,并创建数据库连接

import pymysql

# 使用python连接mysql数据库,并对数据库添加数据的数据的操作
# 创建连接,数据库主机地址 数据库用户名称 密码 数据库名 数据库端口 数据库字符集编码
conn = pymysql.connect(host='127.0.0.1',
user='root',
password='123456',
database='empdb',
port=3307,
charset='utf8')
print("连接成功")

# 创建游标
cursor = conn.cursor()
2、添加一条数据,并提交

# 添加一条数据数据
def insertdata1():
insert_emp_sql = "insert into empdb.employee (ename,age,dept_id,wage_id) values ('王十八',45,1,3)"
# 执行语句
cursor.execute(insert_emp_sql)
# 提交数据
conn.commit()
3、使用for循环,添加插入多条测试数据

# 批量添加数据
def insertdata2():
insert_emp_sql = "insert into empdb.employee (ename,age,dept_id,wage_id) values ('{}',{},1,3);"

# 插入10条数据0-9
for i in range(10):
uname = '高少少'+str(i)
age=30+i

ins_sql= insert_emp_sql.format(uname,age)
cursor.execute(ins_sql)
conn.commit()
4、更改数据

# 更改数据
def updatadata():
updata_emp_sql = "update empdb.employee set age=66 where eid = 26"
cursor.execute(updata_emp_sql)
conn.commit()
5、删除数据

# 删除数据
def deletedata(id):
delete_emp_sql = "delete from empdb.employee where eid={}"
del_sql = delete_emp_sql.format(id)
cursor.execute(del_sql)
conn.commit()

6、关闭游标和连接

def closeconn():
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
完整的代码:

import pymysql

# 使用python连接mysql数据库,并对数据库进行添加数据的方法
# 创建连接,数据库主机地址 数据库用户名称 密码 数据库名 数据库端口 数据库字符集编码
conn = pymysql.connect(host='127.0.0.1',
user='root',
password='123456',
database='empdb',
port=3307,
charset='utf8')
print("连接成功")

# 创建游标
cursor = conn.cursor()

# 添加一条数据数据
def insertdata1():
insert_emp_sql = "insert into empdb.employee (ename,age,dept_id,wage_id) values ('王十八',45,1,3)"
# 执行语句
cursor.execute(insert_emp_sql)
# 提交数据
conn.commit()

# 批量添加数据
def insertdata2():
insert_emp_sql = "insert into empdb.employee (ename,age,dept_id,wage_id) values ('{}',{},1,3);"

# 插入10条数据0-9
for i in range(10):
uname = '高少少'+str(i)
age=30+i

ins_sql= insert_emp_sql.format(uname,age)
cursor.execute(ins_sql)
conn.commit()

# 删除数据
def deletedata(id):
delete_emp_sql = "delete from empdb.employee where eid={}"
del_sql = delete_emp_sql.format(id)
cursor.execute(del_sql)
conn.commit()


# 更改数据
def updatadata():
updata_emp_sql = "update empdb.employee set age=66 where eid = 26"
cursor.execute(updata_emp_sql)
conn.commit()

# 关闭游标跟连接
def closeconn():
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

try:
# insertdata1()
# insertdata2()
deletedata(1)
updatadata()
except:
conn.rollback()

closeconn()
运行结果:

该结果可以在数据库查看

也可以在python里面查看:

使用python连接mysql进行单表查询,python 3.6+pymysql+pandas_yopky的博客-CSDN博客
本文共 个字数,平均阅读时长 ≈ 分钟,您已阅读:0时0分0秒。
3

打赏

评论 (0)

OωO
  • ::(呵呵)
  • ::(哈哈)
  • ::(吐舌)
  • ::(太开心)
  • ::(笑眼)
  • ::(花心)
  • ::(小乖)
  • ::(乖)
  • ::(捂嘴笑)
  • ::(滑稽)
  • ::(你懂的)
  • ::(不高兴)
  • ::(怒)
  • ::(汗)
  • ::(黑线)
  • ::(泪)
  • ::(真棒)
  • ::(喷)
  • ::(惊哭)
  • ::(阴险)
  • ::(鄙视)
  • ::(酷)
  • ::(啊)
  • ::(狂汗)
  • ::(what)
  • ::(疑问)
  • ::(酸爽)
  • ::(呀咩爹)
  • ::(委屈)
  • ::(惊讶)
  • ::(睡觉)
  • ::(笑尿)
  • ::(挖鼻)
  • ::(吐)
  • ::(犀利)
  • ::(小红脸)
  • ::(懒得理)
  • ::(勉强)
  • ::(爱心)
  • ::(心碎)
  • ::(玫瑰)
  • ::(礼物)
  • ::(彩虹)
  • ::(太阳)
  • ::(星星月亮)
  • ::(钱币)
  • ::(茶杯)
  • ::(蛋糕)
  • ::(大拇指)
  • ::(胜利)
  • ::(haha)
  • ::(OK)
  • ::(沙发)
  • ::(手纸)
  • ::(香蕉)
  • ::(便便)
  • ::(药丸)
  • ::(红领巾)
  • ::(蜡烛)
  • ::(音乐)
  • ::(灯泡)
  • ::(开心)
  • ::(钱)
  • ::(咦)
  • ::(呼)
  • ::(冷)
  • ::(生气)
  • ::(弱)
  • ::(狗头)
泡泡
阿鲁
颜文字
取消
  1. 头像
    6767 Lv.1
    iPhone · Safari
    沙发

    画图

    回复
  2. 头像
    6767 Lv.1
    Windows 10 · Google Chrome
    板凳

    表情

    回复
  3. 头像
    透露 Lv.2
    Android · QQ Browser
    第30楼

    649494848

    回复