python读写mysql数据

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()

猜你喜欢

转载自blog.csdn.net/JimmyAndRushking/article/details/82767178
今日推荐