python学习(十二)python3与mysql数据库连接——mysql-connector驱动


前言

本篇主要介绍使用mysql-connector来连接使用mysql。 mysql-connector 是 MySQL 官方提供的驱动器。

一、安装

使用pip命令来安装 mysql-connector:

python -m pip install mysql-connector

测试安装是否成功:

import mysql.connector

如果没有报错,则安装成功。
注意:如果你的 MySQL 是 8.0 版本,密码插件验证方式发生了变化,早期版本为 mysql_native_password,8.0 版本为caching_sha2_password,所以需要做些改变。具体方法可参照网上教程。

二、创建数据库连接

# !/usr/bin/python3
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password'
)
print(conn)

三、创建数据库

创建一个名为runoob_db的数据库,如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password'
)
mycursor = conn.cursor()

mycursor.execute("CREATE DATABASE runoob_db")

四、创建数据表

创建一个名为sites的数据表,如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()

mycursor.execute("CREATE TABLE sites (name VARCHAR(255), url VARCHAR(255))")

五、主键设置

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
mycursor.execute("ALTER TABLE sites ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")

六、插入数据

1、单条插入

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
val = ("RUNOOB", "https://www.runoob.com")
mycursor.execute(sql, val)
 
mydb.commit()    # 数据表内容有更新,必须使用到该语句
 
print(mycursor.rowcount, "记录插入成功。")

2、批量插入

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
val = [
  ('Google', 'https://www.google.com'),
  ('Github', 'https://www.github.com'),
  ('Taobao', 'https://www.taobao.com'),
  ('stackoverflow', 'https://www.stackoverflow.com/')
]
mycursor.executemany(sql, val)
 
mydb.commit()    # 数据表内容有更新,必须使用到该语句
 
print(mycursor.rowcount, "记录插入成功。")

效果如下:
在这里插入图片描述

七、查询数据

1、查询所有数据

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
mycursor.execute("SELECT * FROM sites")
 
myresult = mycursor.fetchall()     # fetchall() 获取所有记录
 
for x in myresult:
  print(x)

输出如下:

('RUNOOB', 'https://www.runoob.com', 1)
('Google', 'https://www.google.com', 2)
('Github', 'https://www.github.com', 3)
('Taobao', 'https://www.taobao.com', 4)
('stackoverflow', 'https://www.stackoverflow.com/', 5)

2、查询指定的字段数据

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
mycursor.execute("SELECT name, url FROM sites")
 
myresult = mycursor.fetchall()
 
for x in myresult:
  print(x)

输出如下:

('RUNOOB', 'https://www.runoob.com')
('Google', 'https://www.google.com')
('Github', 'https://www.github.com')
('Taobao', 'https://www.taobao.com')
('stackoverflow', 'https://www.stackoverflow.com/')

3、查询指定条件的数据

使用where语句,如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
sql = "SELECT * FROM sites WHERE name = %s"
na = ("RUNOOB", )
 
mycursor.execute(sql, na)
 
myresult = mycursor.fetchall()
 
for x in myresult:
  print(x)

输出如下:

('RUNOOB', 'https://www.runoob.com', 1)

4、查询结果排序

查询结果排序可以使用ORDER BY语句,默认的排序方式为升序,关键字为ASC;设置降序,关键字为DESC
按name字段字母的降序排序,如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
sql = "SELECT * FROM sites ORDER BY name DESC"
 
mycursor.execute(sql)
 
myresult = mycursor.fetchall()
 
for x in myresult:
  print(x)

输出如下:

('Taobao', 'https://www.taobao.com', 4)
('stackoverflow', 'https://www.stackoverflow.com/', 5)
('RUNOOB', 'https://www.runoob.com', 1)
('Google', 'https://www.google.com', 2)
('Github', 'https://www.github.com', 3)

5、设置查询的数据量

可以只读取前3条记录,如下:

mycursor.execute("SELECT * FROM sites LIMIT 3")

也可以指定起始位置,如下:

mycursor.execute("SELECT * FROM sites LIMIT 3 OFFSET 1")  # 0 为 第一条,1 为第二条,以此类推

八、删除记录

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
sql = "DELETE FROM sites WHERE name = %s"
na = ("stackoverflow",)

mycursor.execute(sql, na)

conn.commit()

print(mycursor.rowcount, " 条记录删除")

九、更新表数据

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
sql = "UPDATE sites SET name = %s WHERE name = %s"
val = ("TB", "Taobao")

mycursor.execute(sql, val)

conn.commit()

print(mycursor.rowcount, " 条记录被修改")

十、删除表

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user='root',
    password='tamigroup',
    auth_plugin='mysql_native_password',
    database="runoob_db"
)
mycursor = conn.cursor()
sql = "DROP TABLE IF EXISTS sites"  # 删除数据表 sites

mycursor.execute(sql)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码的雪糕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值