ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

菜鸟教程的 mysql-connector 基础

2020-06-03 16:01:47  阅读:2931  来源: 互联网

标签:mycursor execute name 菜鸟 mysql sites connector sql SELECT


安装驱动

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有