找回密码
 立即注册
首页 业界区 安全 2026 1 24

2026 1 24

幌斛者 2026-1-24 18:00:04
1:

sql语句对大小写不敏感;
以分号结束       ;
1.png

2:

--单行注释:
'#'  单行注释:(可以后面不加空格,但推荐加上)
/*  多行注释 */
3 :  DDL 数据库的定义

show databases;
use 数据库的名称
create database 数据库的名称  [charset utf8]
drop database  数据库的名称
select database();   查看当前数据库
4: 表

show tables;
use  helloworld ;
drop table student(表名称);
create table 表名称(
列名称  列类型,
列名称   列类型
);
int flaoat varchar()  date timestamp
2.png

:5: 数据的插入,删除,更新

insert into  表名称(列1,......)values(值1,值2,.....),()
delete from 表名称[where 条件判断]
update 表名称 set 列=值 【where 条件判断】
字符串要用‘’ 括起来
点击查看代码
  1. USE helloworld;
  2. delete from wcnm where name=3;
  3. delete from wcnm;
  4. insert into wcnm values(2,'woman');
  5. update wcnm set name=114514 where name=1;
  6. update wcnm set hex='一样的';
复制代码
6:数据的查询:

select 字段列表 | * from 表  [where ]
点击查看代码
  1. select name,hex from wcnm;
  2. select * from wcnm;
  3. select * from wcnm where name=114514;
复制代码
7:  GROUP BY

3.png

select hex,sum(name),avg(name),min(name),max(name),count(hex),count(*) from wcnm group by hex;
注意: group by 将表中数据按照指定列的值进行分组,把具有相同值的行归为一组
有去重的效果
4.png

5.png

8:结果的排序

6.png

limit  : 限制几条的数据
SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT
点击查看代码
  1. select * from wcnm where name=2 order by hex asc;  # 默让是asc升序
  2. # order by 排序       desc  降序   esc 升序
  3. select * from wcnm where name=2 limit 1;# 限制几条的数据
  4. select * from wcnm where name=2 limit 10,5; #跳过前面的10跳来展示后面的5条
  5. use helloworld;
  6. select namen,cout(*) from wcnm where name>50 group by name  order by name limit 20;
复制代码
9: 联合sql和py

7.png

查询
  1. # 从 pymysql 库中导入 Connection 类(这个类是创建数据库连接的核心模板)
  2. from pymysql import Connection
  3. # 构建 SQL 连接:通过 Connection 类创建实例 conn(即数据库连接对象)
  4. conn = Connection(
  5.     host='localhost',  # 数据库服务器的主机名/IP,localhost 代表本机
  6.     port=3306,         # MySQL 服务的端口号,默认是 3306
  7.     user='root',       # 连接数据库的用户名(root 是 MySQL 超级管理员账户)
  8.     password='123456'  # 连接数据库的密码(需替换为你自己的 MySQL 密码)
  9. )
  10. # 获取并打印 MySQL 服务器的版本信息(用于验证连接是否成功,返回如 8.0.32 这类版本号)
  11. print(conn.get_server_info())
  12. # ------------------------------ 执行 SQL 语句的核心逻辑 ------------------------------
  13. # 1. 获取游标对象:游标(cursor)是执行 SQL 语句的“工具”,所有 SQL 都通过游标执行
  14. cursor = conn.cursor()  
  15. # 2. 选择要操作的数据库:指定后续 SQL 语句执行的目标数据库为 "helloworld"
  16. #    相当于在 MySQL 客户端执行 "USE helloworld;"
  17. conn.select_db("helloworld")
  18. # 【可选】执行非查询类 SQL(增/删/改/建表等),此处注释掉了建表语句
  19. # cursor.execute("create table text_plmysql(id int);")
  20. print("------------------------------")
  21. # 3. 执行查询类 SQL 语句:执行 "select * from wcnm",查询 wcnm 表的所有数据
  22. #    execute 方法接收 SQL 字符串作为参数,负责把 SQL 发送给数据库执行
  23. cursor.execute(" select * from wcnm")
  24. # 4. 获取查询结果:fetchall() 会获取游标执行查询后返回的所有数据,返回格式是 元组嵌套元组
  25. #    比如 ((1, '王磊'), (2, '李娜'), ...),每一个内层元组对应表中的一行数据
  26. results = cursor.fetchall()
  27. # 5. 打印查询结果:输出 wcnm 表的所有数据
  28. print(results)
  29. # 6. 关闭数据库连接:释放数据库的连接资源(必须执行,否则会占用连接池)
  30. #    关闭后 conn 和 cursor 都无法再使用
  31. conn.close()
复制代码
插入
  1. ```plaintext
  2. from pymysql import Connection
  3. # 构建 SQL 连接:通过 Connection 类创建实例 conn(即数据库连接对象)
  4. conn = Connection(
  5.     host='localhost',  # 数据库服务器的主机名/IP,localhost 代表本机
  6.     port=3306,         # MySQL 服务的端口号,默认是 3306
  7.     user='root',       # 连接数据库的用户名(root 是 MySQL 超级管理员账户)
  8.     password='123456', # 连接数据库的密码(需替换为你自己的 MySQL 密码)
  9.     autocommit=True    # 可以不加     默认是commit自己提交
  10. )
  11. # 获取并打印 MySQL 服务器的版本信息(用于验证连接是否成功,返回如 8.0.45 这类版本号)
  12. print(conn.get_server_info())
  13. # ------------------------------ 执行 SQL 语句的核心逻辑 ------------------------------
  14. # 1. 获取游标对象:游标(cursor)是执行 SQL 语句的“工具”,所有 SQL 都通过游标执行
  15. cursor = conn.cursor()
  16. # 2. 选择要操作的数据库:指定后续 SQL 语句执行的目标数据库为 "helloworld"
  17. #    相当于在 MySQL 客户端执行 "USE helloworld;"
  18. conn.select_db("helloworld")
  19. #执行命令
  20. #cursor.execute("insert into wcnm values(101,'老牛')")
  21. cursor.execute("select * from wcnm")
  22. print(cursor.fetchall())
  23. #手动提交
  24. conn.commit()
  25. #关闭
  26. conn.close()
复制代码
[code][/code]
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

2026-1-25 09:15:11

举报

您需要登录后才可以回帖 登录 | 立即注册