一、安装
使用pip命令来安装mysql-connector包,如下:
python -m pip install mysql-connector
测试是否安装,执行以下程序,没有报错则表明安装成功:
import mysql.connector
二、创建数据库连接
1、连接数据库
import mysql.connectormydb = mysql.connector.connect(host="localhost", # 数据库主机地址user="yourusername", # 数据库用户名passwd="yourpassword" # 数据库密码
)print(mydb)
2、创建数据库
mycursor = mydb.cursor()mycursor.execute("CREATE DATABASE runoob_db")
3、查看数据库
(1)使用SHOW DATABASES 查看
mycursor = mydb.cursor()mycursor.execute("SHOW DATABASES")for x in mycursor:print(x)
(2)直接连接数据库,不存在则会报错
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
4、建表
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()mycursor.execute("CREATE TABLE sites (name VARCHAR(255), url VARCHAR(255))")
使用SHOW TABLES来查看表是否存在:
mycursor = mydb.cursor()mycursor.execute("SHOW TABLES")for x in mycursor:print(x)
5、主键设置
一般在建表时就会创建主键,使用 INT AUTO_INCREMENT PRIMARY KEY 创建主键,起始值为1,自增。
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()mycursor.execute("CREATE TABLE sites (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), url VARCHAR(255))")
如果表已经存在,则使用 ATER TABLE 来创建主键。如下:
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()mycursor.execute("ALTER TABLE sites ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")
6、插入数据
使用 INSERT INTO 语句来插入数据:
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.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, "记录插入成功。")
使用mycursor.rowcount来获取插入的条数。
7、批量插入
使用executemany(sql, var)l来做批量数据插入,var是一个元组列表,如下:
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.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, "记录插入成功。")
在插入数据后,使用mycursor.lastrowid获取对于的ID。
8、查询数据
使用SELECT 语句获取:
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()mycursor.execute("SELECT * FROM sites")myresult = mycursor.fetchall() # fetchall() 获取所有记录for x in myresult:print(x)
读取指定字段:
mycursor.execute("SELECT name, url FROM sites")myresult = mycursor.fetchall()for x in myresult:print(x)
读取一条数据,使用fetchone():
mycursor.execute("SELECT * FROM sites")myresult = mycursor.fetchone()print(myresult)
WHERE条件语句:
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()sql = "SELECT * FROM sites WHERE name ='RUNOOB'"mycursor.execute(sql)myresult = mycursor.fetchall()for x in myresult:print(x)
使用通配符%:
sql = "SELECT * FROM sites WHERE url LIKE '%oo%'"mycursor.execute(sql)myresult = mycursor.fetchall()for x in myresult:print(x)
防止SQL注入攻击,可以使用%s作为占位符来转义查询条件:
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()sql = "SELECT * FROM sites WHERE name = %s"
na = ("RUNOOB", )mycursor.execute(sql, na)myresult = mycursor.fetchall()for x in myresult:print(x)
9、排序
对查询结果进行排序,可以使用 ORDER BY,默认为升序(关键字为ASC),也可指定为降序(使用DESC关键字)
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()sql = "SELECT * FROM sites ORDER BY name"mycursor.execute(sql)myresult = mycursor.fetchall()for x in myresult:print(x)
使用LIMIT指定查询数量:
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()mycursor.execute("SELECT * FROM sites LIMIT 3")myresult = mycursor.fetchall()for x in myresult:print(x)
使用OFFSET指定起始位置:
mycursor.execute("SELECT * FROM sites LIMIT 3 OFFSET 1") # 0 为 第一条,1 为第二条,以此类推myresult = mycursor.fetchall()for x in myresult:print(x)
10、删除数据
使用DELETE FROM语句:
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()sql = "DELETE FROM sites WHERE name = 'stackoverflow'"mycursor.execute(sql)mydb.commit()print(mycursor.rowcount, " 条记录删除")
11、更新表数据
使用UPDATE语句
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()sql = "UPDATE sites SET name = 'ZH' WHERE name = 'Zhihu'"mycursor.execute(sql)mydb.commit()print(mycursor.rowcount, " 条记录被修改")
12、删除表
使用 DROP TABLE语句删除表,使用 IF EXISTS 来判断表是否存在:
import mysql.connectormydb = mysql.connector.connect(host="localhost",user="root",passwd="123456",database="runoob_db"
)
mycursor = mydb.cursor()sql = "DROP TABLE IF EXISTS sites" # 删除数据表 sitesmycursor.execute(sql)