- 监测数据采集物联网应用开发步骤(5.2)
静态配置库数据库调用,新建全局变量初始化类com.zxy.main.Init_Page.py
#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''
from com.zxy.z_debug import z_debug
from com.zxy.common.DbConfigSelf import DbConfigSelf
#监测数据采集物联网应用--全局变量初始化
class Init_Page(z_debug):
def __init__(self):
pass
@staticmethod
def Init_Load():
DbConfigSelf.GetDbConfigSelfNew()
Pass
数据库操作测试MonitorDataCmd.py主文件中编写:
from com.zxy.main.Init_Page import Init_Page
from com.zxy.db_Self.Db_Common_Self import Db_Common_Self
if __name__ == '__main__':下编写
#全局变量初始化
Init_Page.Init_Load()
#测试案例
temDbSelf = Db_Common_Self()
#创建数据库表 t_db_config表
strSql = "select count(1) from t_db_config limit 1"
temValue = temDbSelf.Common_Sql(strSql)
if temValue == None:
if Com_Para.driverClassName == "org.sqlite.JDBC":
strSql = "CREATE TABLE t_db_config ("
strSql = strSql+"MAIN_ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
strSql = strSql+"DB_CN_NAME VARCHAR (500) DEFAULT NULL,"
strSql = strSql+"DB_DriverClassName VARCHAR (500) DEFAULT NULL,"
strSql = strSql+"DB_url VARCHAR (500) DEFAULT NULL,"
strSql = strSql+"DB_username VARCHAR (500) DEFAULT NULL,"
strSql = strSql+"DB_password VARCHAR (500) DEFAULT NULL,"
strSql = strSql+"DB_version VARCHAR (500) DEFAULT NULL,"
strSql = strSql+"DB_Code VARCHAR (500) DEFAULT NULL,"
strSql = strSql+"S_DESC VARCHAR (2000) DEFAULT NULL,"
strSql = strSql+"CREATE_DATE datetime NOT NULL DEFAULT (datetime('now')))"
temDbSelf.CommonExec_Sql(strSql)
print("创建数据库表 t_db_config表成功")
#插入数据记录
strSql = "insert into t_db_config(DB_CN_NAME,DB_DriverClassName,DB_url,DB_username,DB_password,DB_version,DB_Code) "
strSql = strSql + " values('测试数据1','测试数据2','测试数据3','测试数据4','测试数据5','测试数据6','测试数据7')"
temDbSelf.CommonExec_Sql(strSql)
print("插入数据记录成功")
#读取数据记录
strSql = "select * from t_db_config limit 2"
temRs = temDbSelf.Common_Sql(strSql)
print(str(temRs))
程序执行成功结果:自动生成center_data.db
打印出数据库数据
小测试:把上文的sql语句故意语法写错执行看看log日志里出现什么变化。
com.zxy.db1和com.zxy.db2的编写参考com.zxy.db_Self,后续功能代码中会使用。
监测数据采集物联网应用开发步骤(6)