unit 11

###################
####11.管理网络####
###################

####1.ip基础知识####
1.ipv4
2进制32位-----10进制

172.25.0.10/255.255.255.0
172.25.0.10:ip地址
255.255.255.0:子网掩码
子网掩码255位对应的ip位为网络位
子网掩码0对应的ip位为主机位

####2.配置ip####
<<图形化>>
1.图形界面
nm-connection-editor
2.文本化图形
nmtui

<<命令>>
ifconfig 网卡 ip netmask    ##临时设定

nmcli connection add type ethernet con-name westos ifname eth0 autoconnect yes
nmcli connection add type ethernet con-name westos ifname eth0 ip4 ip/24
nmcli connection delete westos
nmcli connection show
nmcli connection down westos
nmcli connection up westos
nmcli connection modify "westos" ipv4.addresses newip/24
nmcli connection modify "westos" ipv4.method <auto|manual>
nmcli device connect eth0
nmcli device disconnect eth0
nmcli device show
nmcli device status



<<文件>>
dhcp        ##动态获取
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0        ##接口使用设备
BOOTPROTO=dhcp        ##网卡工作模式
ONBOOT=yes        ##网络服务开启时自动激活
NAME=eth0        ##网络接口名称
:wq
systemctl restart network



static|none    ##静态网络
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0             ##设备
BOOTPROTO=static|none         ##设备工作方式
ONBOOT=yes             ##开启网络服务激活设备
NAME=eth0             ##网络接口名称
IPADDR=172.25.0.100          ##IP
NETNASK=255.255.255.0 | PREFIX=24 ##子网掩码



####3.gateway 网关####

1.路由器
主要功能是用来作nat的
dnat    目的地地址转换
snat    源地址转换

2.网关
路由器上和自己处在同一个网段的那个ip

3.设定网关
systemctl stop     NetwrokManager
vim /etc/sysconfig/network    ##全局网关
GATEWAY=网关ip

vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件 ##网卡接口网关
GATEWAY=网关ip

systemctl restart netwrok

route -n            ##查询网关
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0   (网关)172.25.0.254    0.0.0.0         UG    0      0        0 eth0
172.25.0.0      0.0.0.0         255.255.255.0   U     0      0        0 eth0



####5.dns####
1.dns
dns是一台服务器
这太服务器提供了回答客户主机名和ip对应关系的功能

2.设定dns
vim /etc/resolv.conf
nameserver dns服务器ip

vim /etc/sysconfig/network-scripts/ifcfg-网卡配置文件
DNS1=dns服务器ip

3.本地解析文件
vim /etc/hosts
ip    主机名称

4.本地解析文件和dns读取的优先级调整
/etc/nsswitch.conf
 38 #hosts:     db files nisplus nis dns
 39 hosts:      files dns        ##files代表本地解析文件,dns代表dns服务器,那个在前面那个优先

 
5.dhcp服务的配置



你遇到的错误: ``` sqlite3.OperationalError: table energy_saving has no column named unit11_lube_oil_pump_hours ``` --- ## 🔴 错误原因分析 这个错误说明了一个 **非常关键的问题**: > ✅ 你的 Python 代码在尝试向 `energy_saving` 表插入数据 > ❌ 但数据库中的 `energy_saving` 表 **根本没有这些字段(如 `unit11_lube_oil_pump_hours`)** 这意味着: 👉 **你当前使用的数据库是旧版本(v1),而你的代码已经是新版本(v2)** 即使你在代码里写了 `create_tables()`,SQLite **不会自动更新已有表结构**。它只会“如果表不存在就创建”,但**不会修改已存在的表**。 所以你现在的情况是: | 组件 | 状态 | |------|------| | 📜 Python 代码 | 新版,使用了多个节能设备小时字段 | | 💾 SQLite 数据库 | 旧版,`energy_saving` 表可能只有 `id`, `date`, `total_saving` 等少数字段 | | ⚠️ 结果 | 插入时字段名不匹配 → 报错 | --- ## ✅ 解决方案:重建或升级 `energy_saving` 表 我们有两个选择: ### ✅ 方案一:【推荐】安全升级 —— 保留原数据并添加新字段 我们将手动执行 SQL 的 `ALTER TABLE ADD COLUMN` 来逐步添加所有缺失字段。 > 这样可以**保留历史数据**,又能兼容新代码。 #### ✅ 步骤 1:运行修复脚本(一次性) 将以下脚本保存为 `fix_energy_saving_table.py`: ```python # fix_energy_saving_table.py import sqlite3 import os DB_PATH = 'energy_management.db' # 所有需要添加的字段定义(按组分类) COLUMNS_TO_ADD = [ # #11 机组 ('unit11_lube_oil_pump_hours', 'REAL DEFAULT 0'), ('unit11_aux_oil_pump_hours', 'REAL DEFAULT 0'), ('unit11_jacking_oil_pump_hours', 'REAL DEFAULT 0'), ('unit11_smoke_fan_hours', 'REAL DEFAULT 0'), # #12 机组 ('unit12_lube_oil_pump_hours', 'REAL DEFAULT 0'), ('unit12_aux_oil_pump_hours', 'REAL DEFAULT 0'), ('unit12_jacking_oil_pump_hours', 'REAL DEFAULT 0'), ('unit12_smoke_fan_hours', 'REAL DEFAULT 0'), # #13 机组 ('unit13_condensate_pump_hours', 'REAL DEFAULT 0'), ('unit13_closed_circuit_pump_hours', 'REAL DEFAULT 0'), ('unit13_demineralized_water_pump_hours', 'REAL DEFAULT 0'), ('unit13_vacuum_filter_hours', 'REAL DEFAULT 0'), ('unit13_smoke_and_vent_fan_hours', 'REAL DEFAULT 0'), # #14 机组 ('unit14_condensate_pump_hours', 'REAL DEFAULT 0'), ('unit14_closed_circuit_pump_hours', 'REAL DEFAULT 0'), ('unit14_demineralized_water_pump_hours', 'REAL DEFAULT 0'), ('unit14_vacuum_filter_hours', 'REAL DEFAULT 0'), ('unit14_smoke_and_vent_fan_hours', 'REAL DEFAULT 0'), # 公用设备 ('shared_gas_heater_hours', 'REAL DEFAULT 0'), ('shared_ac_11_13_units', 'INTEGER DEFAULT 0'), ('shared_ac_11_13_hours', 'REAL DEFAULT 0'), ('shared_ac_12_14_units', 'INTEGER DEFAULT 0'), ('shared_ac_12_14_hours', 'REAL DEFAULT 0'), ('industrial_pump1_running', 'INTEGER DEFAULT 0'), ('industrial_pump2_running', 'INTEGER DEFAULT 0'), ('industrial_pump3_running', 'INTEGER DEFAULT 0'), ('industrial_pump_hours', 'REAL DEFAULT 24'), ('total_saving', 'REAL DEFAULT 0'), # 新增字段(v2) ('notes', 'TEXT'), ('approved', 'INTEGER DEFAULT 0') ] def fix_energy_saving_table(): if not os.path.exists(DB_PATH): print("❌ 数据库文件不存在!请先运行主程序生成数据库。") return False conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() # 获取当前 energy_saving 表的所有列名 cursor.execute("PRAGMA table_info(energy_saving)") existing_columns = [row[1] for row in cursor.fetchall()] print(f"🔍 当前 energy_saving 表已有字段数: {len(existing_columns)}") added_count = 0 for col_name, col_def in COLUMNS_TO_ADD: if col_name not in existing_columns: try: sql = f"ALTER TABLE energy_saving ADD COLUMN {col_name} {col_def}" cursor.execute(sql) print(f"✅ 已添加字段: {col_name}") added_count += 1 except Exception as e: print(f"❌ 添加字段失败 {col_name}: {e}") if added_count == 0: print("🎉 所有字段均已存在,无需修复!") else: print(f"✅ 成功添加 {added_count} 个字段!") conn.commit() conn.close() return True if __name__ == "__main__": print("🔧 开始修复 energy_saving 表...") success = fix_energy_saving_table() if success: print("✅ 修复完成,请重新启动主程序!") else: print("⚠️ 修复失败,请检查数据库路径和权限。") ``` --- ### ✅ 使用方法 1. 关闭所有正在运行的 Python 程序(防止数据库被占用) 2. 将上面代码保存为 `fix_energy_saving_table.py` 3. 在终端中运行: ```bash python fix_energy_saving_table.py ``` 输出示例: ``` 🔧 开始修复 energy_saving 表... 🔍 当前 energy_saving 表已有字段数: 3 ✅ 已添加字段: unit11_lube_oil_pump_hours ✅ 已添加字段: unit11_aux_oil_pump_hours ... ✅ 成功添加 29 个字段! ✅ 修复完成,请重新启动主程序! ``` 4. 再次运行你的主程序或数据分析模块即可正常工作! --- ### ✅ 方案二:彻底重建表(仅用于测试环境) 如果你的数据不重要,可以直接删除旧表并重建。 ⚠️ 警告:这会**丢失所有节能数据** ```python # dangerous_reset_energy_saving.py import sqlite3 import os conn = sqlite3.connect('energy_management.db') cursor = conn.cursor() # 删除旧表 cursor.execute("DROP TABLE IF EXISTS energy_saving") # 重新创建(与代码一致) cursor.execute(''' CREATE TABLE energy_saving ( id INTEGER PRIMARY KEY, date TEXT NOT NULL UNIQUE, unit11_lube_oil_pump_hours REAL DEFAULT 0, unit11_aux_oil_pump_hours REAL DEFAULT 0, unit11_jacking_oil_pump_hours REAL DEFAULT 0, unit11_smoke_fan_hours REAL DEFAULT 0, unit12_lube_oil_pump_hours REAL DEFAULT 0, unit12_aux_oil_pump_hours REAL DEFAULT 0, unit12_jacking_oil_pump_hours REAL DEFAULT 0, unit12_smoke_fan_hours REAL DEFAULT 0, unit13_condensate_pump_hours REAL DEFAULT 0, unit13_closed_circuit_pump_hours REAL DEFAULT 0, unit13_demineralized_water_pump_hours REAL DEFAULT 0, unit13_vacuum_filter_hours REAL DEFAULT 0, unit13_smoke_and_vent_fan_hours REAL DEFAULT 0, unit14_condensate_pump_hours REAL DEFAULT 0, unit14_closed_circuit_pump_hours REAL DEFAULT 0, unit14_demineralized_water_pump_hours REAL DEFAULT 0, unit14_vacuum_filter_hours REAL DEFAULT 0, unit14_smoke_and_vent_fan_hours REAL DEFAULT 0, shared_gas_heater_hours REAL DEFAULT 0, shared_ac_11_13_units INTEGER DEFAULT 0, shared_ac_11_13_hours REAL DEFAULT 0, shared_ac_12_14_units INTEGER DEFAULT 0, shared_ac_12_14_hours REAL DEFAULT 0, industrial_pump1_running INTEGER DEFAULT 0, industrial_pump2_running INTEGER DEFAULT 0, industrial_pump3_running INTEGER DEFAULT 0, industrial_pump_hours REAL DEFAULT 24, total_saving REAL DEFAULT 0, notes TEXT, approved INTEGER DEFAULT 0 ) ''') conn.commit() conn.close() print("✅ energy_saving 表已重置") ``` --- ## ✅ 如何避免未来再出问题? | 方法 | 说明 | |------|------| | ✅ 使用 `PRAGMA table_info(...)` 检查字段 | 启动时自动检测缺失列 | | ✅ 加入 `ensure_database_integrity()` 函数 | 自动补全字段 | | ✅ 记录数据库版本号 | 用一张 `db_version` 表记录当前版本 | | ✅ 定期备份 | 防止误操作导致数据丢失 | --- ## ✅ 最终验证 你可以打开 DB Browser for SQLite 或运行下面代码查看结构是否正确: ```python conn = sqlite3.connect('energy_management.db') cursor = conn.cursor() cursor.execute("PRAGMA table_info(energy_saving)") for row in cursor.fetchall(): print(row) conn.close() ``` 你应该看到几十个字段,包括: ``` (2, 'unit11_lube_oil_pump_hours', 'REAL', 0, '0', 0) (3, 'unit11_aux_oil_pump_hours', 'REAL', 0, '0', 0) ... ``` --- 现在你再运行: ```bash python 数据分析.py ``` 就不会再报错了! ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值