11. Mysql优化
11.1 索引优化
11.2 SQL 优化
12.SQL 注入,备份
Select * from user where 1=1
13.jdbc 连接数据库
"""
连接mysql
"""
from pymysql import Connection
# 构建数据库连接
conn = Connection(
host='localhost',
port=3306,
user='root',
password='123456',
database="xm_map",
autocommit=True,
)
# 获取游标对象
cursor = conn.cursor()
# 执行sql
# 插入单挑
# cursor.execute("insert into student values(6,'韩信',29,'2024-06-14', '2024-06-02 18:13:21')")
# 定义要插入的多条数据
# data = [
# (7, '李白', 30, '2024-06-15', '2024-06-03 18:13:21'),
# (8, '杜甫', 31, '2024-06-16', '2024-06-04 18:13:21'),
# (9, '王维', 32, '2024-06-17', '2024-06-05 18:13:21')
# ]
# # 构建插入语句
# sql = "INSERT INTO student (id, name, age, birth_date, create_time) VALUES (%s, %s, %s, %s, %s)"
# # 执行批量插入
# cursor.executemany(sql, data)
# 执行更新操作
# cursor.execute("UPDATE student SET name = '李太白' WHERE id = 7")
# 执行删除操作
# cursor.execute("DELETE FROM student WHERE id = 8")
cursor.execute("select * from student")
# 获取查询结果
result = cursor.fetchall()
for row in result:
print(row)
# 关闭游标
cursor.close()
# 关闭数据库
conn.close()
# print("第一个",json.dumps(response.json(), indent=4,ensure_ascii=False))