使用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博客

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐