当前位置: 首页 > news >正文

5、监测数据采集物联网应用开发步骤(5.1)

  1. 监测数据采集物联网应用开发步骤(4)

Sqlite3数据库读写操作开发、异常信息统一处理类开发

本章节需要调用sqlite3及mysql-connector

安装sqlite3

Pip3 install sqlite3

安装mysql-connector

pip3 install mysql-connector

验证是否安装成功,python中运行下列代码无异常则安装成功:

import sqlite3
import mysql.connector

在项目开发过程中各类异常信息try...except处理,该处理类将try ...except异常信息统一处理并日志文件输出,便于快速定位错误信息及代码位置;

异常信息日志打印格式

yyyy-MM-dd H:mm:ss=>异常的类名=>该类中调用出错的函数名==>其他异常信息

参考如下图:

创建异常信息统一处理类com.zxy.z_debug.py

调用方式参考:

from com.zxy.z_debug import z_debug
class ClassName(z_debug):

com.zxy.z_debug.py类

#! python3
# -*- coding: utf-8 -
'''
Created on 2023年08月28日
@author: zxyong 13738196011
'''import inspect,datetime
from com.zxy.adminlog.UsAdmin_Log import UsAdmin_Log
from com.zxy.common import Com_Para#监测数据采集物联网应用--异常信息统一处理
class z_debug(object):def __init__(self):passdef debug_in(self,inputMsg):excStr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"=>"+str(self)+"=>"+self.__class__.__name__+"."+inspect.stack()[1][3]+"==>"+inputMsguL = UsAdmin_Log(Com_Para.ApplicationPath, excStr)uL.SaveFileDaySub("exception")print(excStr)def debug_info(self):excStr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"=>"+str(self)+"=>"+self.__class__.__name__+"."+inspect.stack()[1][3]+"==>"uL = UsAdmin_Log(Com_Para.ApplicationPath, excStr)uL.SaveFileDaySub("exception_debug")return excStr

在多个线程中使用访问SQLite数据库,为避免多个线程之间的竞争。需要添加数据库线程锁机制以确保线程安全,若使用mysql数据库无需数据库线程锁机制。

com.zxy.common.Com_Para.py中添加如下内容

#数据库线程锁机制
Dblock1 = threading.Lock()
Dblock2 = threading.Lock()
Dblock3 = threading.Lock()
Dblock4 = threading.Lock()
#静态数据库配置文件路径
ACTIONPATH = "dbconfig.properties"
driverClassName = ""
url = ""
username = ""
password = ""
#是否桌面数据库 0:Mysql 1:sqlite3
IL_DB = 1
#数据库连接池初始数量 1:Sqlite3
incrementalConnections = 2
#数据库连接池最大数量
maxConnections = 10
port = -1
UserDebug = "0"

创建静态配置库数据库配置文件读写类com.zxy.common.DbConfigSelf.py

#! python3
# -*- coding: utf-8 -
'''
Created on 2023年08月28日
@author: zxyong 13738196011
'''import configparser
from com.zxy.common import Com_Para
from com.zxy.z_debug import z_debug#监测数据采集物联网应用--静态配置库数据库配置文件读写
class DbConfigSelf(z_debug):def __init__(self):pass@staticmethoddef GetDbConfigSelfNew():temPath = Com_Para.ApplicationPath + Com_Para.zxyPath + Com_Para.ACTIONPATHtemProp = configparser.ConfigParser()try:temProp.read(temPath)Com_Para.driverClassName = temProp.get("DB", "dataSource.driverClassName")            if Com_Para.driverClassName == "org.sqlite.JDBC" :Com_Para.incrementalConnections = 1Com_Para.maxConnections = 1Com_Para.IL_DB = 1temStrRem = temProp.get("DB","dataSource.url").replace("@",Com_Para.ApplicationPath + Com_Para.zxyPath)Com_Para.url = temStrRemelse:Com_Para.url = temProp.get("DB", "dataSource.url")Com_Para.username = temProp.get("DB", "dataSource.username")Com_Para.password = temProp.get("DB", "dataSource.password")Com_Para.port = temProp.getint("DB", "server.port")Com_Para.UserDebug = temProp.getint("DB", "userDebug")except Exception as e:print("GetDbConfigSelfNew:"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))finally:Pass

创建静态配置库数据库配置文件dbconfig.properties

配置文件内容:

[DB]
dataSource.driverClassName=org.sqlite.JDBC
dataSource.url=@center_data.db
dataSource.username=
dataSource.password=server.port=9000
userDebug=0
  1. 监测数据采集物联网应用开发步骤(5.2)
http://www.lryc.cn/news/144746.html

相关文章:

  • ZZULIOJ 1148: 组合三位数之一,Java
  • ROS功能包目录下CMakeLists.txt
  • Python爬虫追踪新闻事件发展进程及舆论反映
  • block层:7. 请求下发
  • Matlab图像处理-平移运算
  • 美创科技一体化智能化公共数据平台数据安全建设实践
  • 关于单例模式
  • pytest笔记: pytest单元测试框架
  • vulnhub Seattle-0.0.3
  • MYSQL 添加行号将行号写入到主键的列
  • 前端命令npm 、 cnpm、 pnpm、yarn 、 npx、nvm的区别
  • Linux 发行版 Debian 宣布支持龙芯 LoongArch 架构
  • PConv : Run, Don’t Walk: Chasing Higher FLOPS for Faster Neural Networks
  • Python中怎么解决内存管理的问题? - 易智编译EaseEditing
  • 【JavaEE】Spring事务-事务的基本介绍-事务的实现-@Transactional基本介绍和使用
  • CentOs下面安装jenkins记录
  • 海康威视相机-LINUX SDK 开发
  • AI助力智能安检,基于图像目标检测实现危险品X光智能安全检测系统
  • 开源软件的崛起:历史与未来
  • apk 静默安装
  • Unity记录4.2-存储-从json文件获取Tile路径
  • vue3页面传参?
  • NB水表和LoRa水表有哪些不同之处?
  • Java进阶(6)——抢购问题中的数据不安全(非原子性问题) Java中的synchronize和ReentrantLock锁使用 死锁及其产生的条件
  • SpringBoot初级开发--加入Log4j进行日志管理打印(6)
  • 计算机竞赛 基于GRU的 电影评论情感分析 - python 深度学习 情感分类
  • android logcat问题 怎么换成旧版
  • 监听的用法watch
  • XML—标记语言
  • 图数据库Neo4j学习五渲染图数据库neo4jd3