python读写mysql数据

本文介绍了如何使用Python3的pymysql库来读取和写入MySQL数据库。首先,通过pip安装pymysql。接着,演示了在guest数据库的login表中读取数据,包括使用fetchall()和fetchone()方法获取数据。然后,讲解了如何写入数据,包括创建表、添加、更新、删除和查询操作,所有操作无需显式提交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python3读取mysql数据或写入数据到mysql中,需要安装pymysql支持库。安装命令:pip install pymysql.

python2操作mysql数据的支持库是MySQLdb,本文采用python3。

一、读取mysql数据
1.数据库准备

在guest数据库中建login表,并向表中插入数据。

在这里插入图片描述


2.关键函数

pymysql.connect()  #打开数据库连接
db.cursor()  #创建游标对象
cursor.execute(sql)  #执行sql语句
cursor.fetchall()   #查询全部数据
cursor.fetchone()   #获取一条数据
db.close()  #关掉数据库连接

3.完整代码

import pymysql


def read_data(sql):
    host = 'localhost'  #ip
    port = 3305  #端口号
    user = 'root'   #数据库用户名
    password = '123456'    #数据库密码
    db = 'guest'    #数据库名
    charset = 'utf8mb4'  #编码

    try:
        db = pymysql.connect(host=host, port=port, user=user, password=password, db=db, charset=charset)  #打开数据库连接
        cursor = db.cursor()  #创建游标对象

        line = cursor.execute(sql)   #执行sql,返回行数
        data = cursor.fetchall()   #fetchall返回查询的全部数据, 返回元组类型数据
        # data = cursor.fetchone()   #fetchone只获取一条数据, 返回元组类型数据
        # print(line)
        print(data)

    except Exception as msg:
        print(msg)
    finally:
        cursor.close()  #关掉游标
        db.close()  #关掉数据库连接


if __name__ == '__main__':
    sql = "select * from login"  # 数据库语句
    read_data(sql)

4.fetchone() 和 fetchall() 查询数据数据

查询数据库中全部数据:

sql = "select * from login"  # 数据库语句

1)fetchall()

data = cursor.fetchall()   #fetchall返回查询的全部数据, 返回元组类型数据
print(data)

查询结果图:

在这里插入图片描述

2)fetchone()

data = cursor.fetchone()   #fetchone只获取一条数据, 返回元组类型数据
print(data)

查询结果图:

在这里插入图片描述

二、写入数据到mysql中

1.关键函数

db.commit()  #提交到数据库执行

其余同读取数据中的关键函数。

2.关键语句

1)创建表

sql_table = 'create table if not exists student(id int(8), name varchar(12))'  #创建表
cursor.execute(sql_table)  #执行sql语句

创建表可以不提交到数据库

2)添加数据

add_data = "insert into student(id, name) values" \
            "(1001, 'zhangshan')," \
            "(1002, 'lisi')," \
            "(1003, 'wangwu')"  #添加数据sql语句
cursor.execute(sql)    #执行sql语句
db.commit()  #提交到数据库执行

3)更新数据

update_data = "update student set name = 'zhaoliu' where id = 1001"  #更新数据sql语句
cursor.execute(sql)    #执行sql语句
db.commit()  #提交到数据库执行

4)删除数据

delete_data = "delete from student where id = 1001"  #删除数据sql语句
cursor.execute(sql)    #执行sql语句
db.commit()  #提交到数据库执行

5)查询数据

sql = "select * from student" #查询数据
cursor.execute(sql)   #执行sql语句
data =cursor.fetchall()  #查询全部数据
print(data)  #执行sql语句

​ 查询数据不用提交到数据库

3.完整代码

import pymysql


def write_data():
    host = 'localhost'  #ip
    port = 3305  #端口号,一般端口号未3306
    user = 'root'   #数据库用户名
    password = '123456'    #数据库密码
    database = 'guest'    #数据库名
    charset = 'utf8mb4'  #编码

    db = pymysql.connect(host=host, port=port, user=user, password=password, db=database, charset=charset)  # 打开数据库连接
    cursor = db.cursor()  # 创建游标对象

    try:
        #创建表sql语句
        # sql_table = 'create table if not exists student(id int(8), name varchar(12))' 
        # cursor.execute(sql_table)  #执行sql语句

        # add_data = "insert into student(id, name) values" \
        #            "(1001, 'zhangshan')," \
        #            "(1002, 'lisi')," \
        #            "(1003, 'wangwu')"  #添加数据sql语句

        # delete_data = "delete from student where id = 1001"  #删除数据sql语句

        # sql = "select * from student" #查询数据
        # cursor.execute(sql)
        # data =cursor.fetchall()
        # print(data)  #执行sql语句

        update_data = "update student set name = 'zhaoliu' where id = 1001"  #更新数据
        cursor.execute(update_data)   #add_data, delete_data, update_data
        db.commit()  # 提交到数据库执行,添加数据、更新数据、删除数据 需要commit

    except Exception as msg:
        # 发生错误时回滚
        db.rollback()
        print(msg)
    finally:
        cursor.close()  #关掉游标
        db.close()  #关掉数据库连接


if __name__ == '__main__':
    write_data()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值