幌斛者 发表于 2026-1-24 18:00:04

2026 1 24

1:

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

2:

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

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

show tables;
usehelloworld ;
drop table student(表名称);
create table 表名称(
列名称列类型,
列名称   列类型
);
int flaoat varchar()date timestamp

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

insert into表名称(列1,......)values(值1,值2,.....),()
delete from 表名称
update 表名称 set 列=值 【where 条件判断】
字符串要用‘’ 括起来
点击查看代码USE helloworld;
delete from wcnm where name=3;
delete from wcnm;

insert into wcnm values(2,'woman');

update wcnm set name=114514 where name=1;

update wcnm set hex='一样的';6:数据的查询:

select 字段列表 | * from 表
点击查看代码select name,hex from wcnm;
select * from wcnm;
select * from wcnm where name=114514;7:GROUP BY


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


8:结果的排序


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


查询# 从 pymysql 库中导入 Connection 类(这个类是创建数据库连接的核心模板)
from pymysql import Connection

# 构建 SQL 连接:通过 Connection 类创建实例 conn(即数据库连接对象)
conn = Connection(
    host='localhost',# 数据库服务器的主机名/IP,localhost 代表本机
    port=3306,         # MySQL 服务的端口号,默认是 3306
    user='root',       # 连接数据库的用户名(root 是 MySQL 超级管理员账户)
    password='123456'# 连接数据库的密码(需替换为你自己的 MySQL 密码)
)

# 获取并打印 MySQL 服务器的版本信息(用于验证连接是否成功,返回如 8.0.32 这类版本号)
print(conn.get_server_info())

# ------------------------------ 执行 SQL 语句的核心逻辑 ------------------------------
# 1. 获取游标对象:游标(cursor)是执行 SQL 语句的“工具”,所有 SQL 都通过游标执行
cursor = conn.cursor()

# 2. 选择要操作的数据库:指定后续 SQL 语句执行的目标数据库为 "helloworld"
#    相当于在 MySQL 客户端执行 "USE helloworld;"
conn.select_db("helloworld")

# 【可选】执行非查询类 SQL(增/删/改/建表等),此处注释掉了建表语句
# cursor.execute("create table text_plmysql(id int);")

print("------------------------------")

# 3. 执行查询类 SQL 语句:执行 "select * from wcnm",查询 wcnm 表的所有数据
#    execute 方法接收 SQL 字符串作为参数,负责把 SQL 发送给数据库执行
cursor.execute(" select * from wcnm")

# 4. 获取查询结果:fetchall() 会获取游标执行查询后返回的所有数据,返回格式是 元组嵌套元组
#    比如 ((1, '王磊'), (2, '李娜'), ...),每一个内层元组对应表中的一行数据
results = cursor.fetchall()

# 5. 打印查询结果:输出 wcnm 表的所有数据
print(results)

# 6. 关闭数据库连接:释放数据库的连接资源(必须执行,否则会占用连接池)
#    关闭后 conn 和 cursor 都无法再使用
conn.close()插入```plaintext
from pymysql import Connection

# 构建 SQL 连接:通过 Connection 类创建实例 conn(即数据库连接对象)
conn = Connection(
    host='localhost',# 数据库服务器的主机名/IP,localhost 代表本机
    port=3306,         # MySQL 服务的端口号,默认是 3306
    user='root',       # 连接数据库的用户名(root 是 MySQL 超级管理员账户)
    password='123456', # 连接数据库的密码(需替换为你自己的 MySQL 密码)
    autocommit=True    # 可以不加   默认是commit自己提交
)

# 获取并打印 MySQL 服务器的版本信息(用于验证连接是否成功,返回如 8.0.45 这类版本号)
print(conn.get_server_info())

# ------------------------------ 执行 SQL 语句的核心逻辑 ------------------------------
# 1. 获取游标对象:游标(cursor)是执行 SQL 语句的“工具”,所有 SQL 都通过游标执行
cursor = conn.cursor()

# 2. 选择要操作的数据库:指定后续 SQL 语句执行的目标数据库为 "helloworld"
#    相当于在 MySQL 客户端执行 "USE helloworld;"
conn.select_db("helloworld")
#执行命令
#cursor.execute("insert into wcnm values(101,'老牛')")

cursor.execute("select * from wcnm")
print(cursor.fetchall())
#手动提交
conn.commit()
#关闭
conn.close()
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

尸酒岐 发表于 2026-1-25 09:15:11

新版吗?好像是停更了吧。

许娴广 发表于 2026-1-25 12:37:13

鼓励转贴优秀软件安全工具和文档!

嗦或 发表于 2026-1-28 07:10:25

感谢,下载保存了

纪音悦 发表于 2026-2-2 09:25:15

yyds。多谢分享

东门芳洲 发表于 2026-2-3 04:46:45

这个有用。

聊账 发表于 2026-2-5 11:40:59

感谢分享,下载保存了,貌似很强大

劳暄美 发表于 2026-2-7 21:07:13

鼓励转贴优秀软件安全工具和文档!

撷监芝 发表于 2026-2-8 11:02:46

这个有用。

拓炊羡 发表于 2026-2-9 02:41:30

感谢分享

上官银柳 发表于 2026-2-9 03:33:04

这个有用。

染罕习 发表于 2026-2-9 06:10:46

不错,里面软件多更新就更好了

万俟谷雪 发表于 2026-2-9 23:19:59

过来提前占个楼

挽幽 发表于 2026-2-11 22:34:44

yyds。多谢分享

何玲 发表于 2026-2-13 06:32:19

这个有用。

咚獭 发表于 2026-2-13 08:42:36

分享、互助 让互联网精神温暖你我

痨砖 发表于 2026-2-17 04:01:08

这个好,看起来很实用

忙贬 发表于 2026-3-11 05:49:19

很好很强大我过来先占个楼 待编辑

荆邦 发表于 2026-3-11 08:35:41

感谢,下载保存了
页: [1]
查看完整版本: 2026 1 24