zoukankan      html  css  js  c++  java
  • python中mysql管理模块mysql-connector使用

    一.  安装第三方管理模块
    首先安装第三方mysql管理模块:pip install mysql-connector.如果安装过程中出现
    WARNING: You are using pip version 21.3; however, version 21.3.1 is available.
    You should consider upgrading via the 'C:Program Filespython3.9python.exe -m pip install --upgrade pip' command.
    说明pip的版本有新的了,可以升级一下,命令:python.exe -m pip install --upgrade pip
    如果直接用官方的源安装速度太慢的话可以使用清华或者阿里的源,更改方法如下:
    a. 临时使用镜像源下载Python包:
    以numpy为例:pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
    其他镜像源地址:
    阿里云:http://mirrors.aliyun.com/pypi/simple/
    豆瓣:http://pypi.douban.com/simple/
    b. 永久更改镜像源:
    在"C:Users用户名"文件夹下,添加pip文件夹,文件夹内新建pip.ini文件,内容为:
    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    安装完成后可以,在交互式界面直接import测试看一下,导入的模块import mysql.connector 注意是点不是横杠。
    二. 创建数据库连接
    语法:connect(host,user,passwd,database,auth_plugin = "mysql_native_password")auth_plugin为mysql密码加密认证方式,
    如果认证方式报错,出现如下提示:
    mysql Authentication plugin ‘caching_sha2_password’ is not supported问题
    是因为在MySQL 8.0以后,默认的密码加密方式是caching_sha2_password而不是mysql_native_password。
    解决方法:
    1.登录mysql数据库 mysql -u root -p
    2.更新身份认证方式 alter user root@localhost identified with mysql_native_password by '密码';
    3.查看修改结果:
    use mysql;
    select * from userG;
    4.修改后的加密方式:
    engine = create_engine(
    "mysql+mysqlconnector://username:password@host:port/database?auth_plugin=mysql_native_password"
    )
    数据库连接成功后,可以打印输出mysql.connector对象的地址!
    三. 数据库的操作
    A.单条插入语句;
    生成一个cursor对象:(mysql.connector对象).cursor();例如:
    con=mysql.connector.connect(host="mysql地址",user="用户名",passwd="密码",database="数据库名“,auth_plugin = "mysql_native_password")
    my_cursor=con.cursor()
    编写sql语句
    sql="insert into 表名(字段1,字段2...) values (%s,%s....)" 在一个表里添加数据,%s只是一个占位符
    val=(具体数值1,具体数值2....)
    执行sql语句
    my_cursor.execute(sql,val)
    提交
    con.commit()
    print(my_cursor.rowcount(插入记录统计),"插入记录成功!")
    B.批量插入数据;
    批量插入数据操作步骤:
    获取连接对象-->获取cursor对象-->编写sql语句-->使用列表赋值-->调用executemany()执行sql语句-->提交事务 例
    # 获取连接对象
    con=mysql.connector.connect(host='localhost',user='root',passwd='123',database='test01',auth_plugin = "mysql_native_password")
    # print(con)
    # 获取cursor对象
    my_cursor=con.cursor()
    #编写sql语句
    sql="insert into ss(name,classroom,yuwen,shuxue) values (%s,%s,%s,%s)"
    # 使用列表赋值
    vals=[
    ('赵大','10班',90,92),
    ('赵二','11班',87,89),
    ('赵三','10班',93,23),
    ('赵四','11班',80,90),
    ('赵五','11班',70,0),
    ]
    #调用executemany()执行sql语句
    my_cursor.executemany(sql,vals)
    #提交事务
    con.commit()
    print(my_cursor.rowcount,"条记录插入成功!")
    C.更新和删除数据的操作
    更新删除数据操作步骤:获取连接对象-->获取cursor对象-->编写sql语句-->调用executemany()执行sql语句-->提交事务
    # 获取连接对象
    con=mysql.connector.connect(host='localhost',user='root',passwd='123',database='test01',auth_plugin = "mysql_native_password")
    # print(con)
    # 获取cursor对象
    my_cursor=con.cursor()
    #编写sql语句
    sql="update ss set name='刘飞' where num=13" 更新数据
    sql='delete from ss where num=12' #删除语句
    #调用executemany()执行sql语句
    my_cursor.execute(sql)
    #提交事务
    con.commit()
    print(my_cursor.rowcount,"条记录修改成功!")
    D.查询操作
    查询数据操作步骤:获取连接对象-->获取cursor对象-->编写sql语句-->调用executemany()执行sql语句-->提交事务--执行fetchall方法获取返回结果
    结果为列表类型,遍历列表数据 例题:
    import mysql.connector
    # 获取连接对象
    con=mysql.connector.connect(host='localhost',user='root',passwd='123',database='test01',auth_plugin = "mysql_native_password")
    # print(con)
    # 获取cursor对象
    my_cursor=con.cursor()
    #编写sql#编写sql语句
    sql='select * from ss where yuwen>70'
    my_cursor.execute(sql)
    my_re=my_cursor.fetchall()
    print(type(my_re))
    for i in my_re:
    print(i)
  • 相关阅读:
    spring(1)
    mybatis(7)自定义结果集(一对多/多对一)
    延迟加载
    《构建之法》阅读笔记03
    http socket
    转换
    .net后台通过xmlhttp 和远程服务通讯
    XMLHttpRequest介绍
    js 贪吃蛇
    触发器
  • 原文地址:https://www.cnblogs.com/Centwei/p/15486917.html
Copyright © 2011-2022 走看看