数据库
首页 > 数据库> > 菜鸟教程的 mysql-connector 基础

菜鸟教程的 mysql-connector 基础

作者:互联网

安装驱动

python -m pip install mysql-connector
导包

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",       # 数据库主机地址
  user="root",    # 数据库用户名
  passwd="root"   # 数据库密码
)
创建游标

mycursor = mydb.cursor()
使用 mycursor.execute("sql 语句") 进行运行

mycursor.execute("CREATE DATABASE runoob_db")
指定数据库名为 runoob_db


mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="123456",
  database="runoob_db"
)
创建数据表

mycursor.execute("CREATE TABLE sites (name VARCHAR(255), url VARCHAR(255))")
查看当前数据表有哪些

mycursor.execute("SHOW TABLES")
使用 "INT AUTO_INCREMENT PRIMARY KEY" 语句
创建一个主键,主键起始值为 1,逐步递增

mycursor.execute("ALTER TABLE sites ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")
创建表时,添加主键

mycursor.execute("CREATE TABLE sites (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), url VARCHAR(255))")
插入数据

sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
val = ("RUNOOB", "https://www.runoob.com")
mycursor.execute(sql, val)
 
mydb.commit()    # 数据表内容有更新,必须使用到该语句
打印 行号

mycursor.rowcount
插入多条语句

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()    # 数据表内容有更新,必须使用到该语句
在数据插入后,获取该条记录的 ID

mycursor.lastrowid
使用  fetchall() 获取所有记录

mycursor.execute("SELECT * FROM sites")
 
myresult = mycursor.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 语句

sql = "SELECT * FROM sites WHERE name ='RUNOOB'"
 
mycursor.execute(sql)
 
myresult = mycursor.fetchall()
使用 fetchall 之后,需要使用循环进行输出

for x in myresult:
  print(x)
使用 通配符 % 
sql = "SELECT * FROM sites WHERE url LIKE '%oo%'"
使用 %s 防止发生 SQL 注入攻击

sql = "SELECT * FROM sites WHERE name = %s"
na = ("RUNOOB", )
 
mycursor.execute(sql, na)
排序

使用 ORDER BY 语句,默认升序,关键字为 ASC

如果要设置降序排序,可以设置关键字 DESC
sql = "SELECT * FROM sites ORDER BY name"
 
mycursor.execute(sql)
降序 DESC

sql = "SELECT * FROM sites ORDER BY name DESC"
 
mycursor.execute(sql)
使用 limit 设置查询的数据量

mycursor.execute("SELECT * FROM sites LIMIT 3")
limit 指定起始位置 使用 offset

mycursor.execute("SELECT * FROM sites LIMIT 3 OFFSET 1")  
# 0 为 第一条,1 为第二条,以此类推
 
myresult = mycursor.fetchall()
删除记录 delete from

sql = "DELETE FROM sites WHERE name = 'stackoverflow'"
 
mycursor.execute(sql)
sql = "DELETE FROM sites WHERE name = %s"
na = ("stackoverflow", )
 
mycursor.execute(sql, na)
更新表中数据  update

sql = "UPDATE sites SET name = 'ZH' WHERE name = 'Zhihu'"
 
mycursor.execute(sql)
sql = "UPDATE sites SET name = %s WHERE name = %s"
val = ("Zhihu", "ZH")
 
mycursor.execute(sql, val)
删除表 drop table

可以先使用 if exists 判断是否存在

sql = "DROP TABLE IF EXISTS sites"  # 删除数据表 sites
 
mycursor.execute(sql)

2020-06-03

标签:mycursor,execute,name,菜鸟,mysql,sites,connector,sql,SELECT
来源: https://www.cnblogs.com/hany-postq473111315/p/13037118.html