在前面的《基于DragonBoard 410c的家庭智能环保卫士——(9)数据库设计》中,本文给出个了系统主要的数据表的设计方案,并且采用powerDesign完成了相关表的绘制,这里进一步采用python脚本完成整改数据库物理层的设计,给出具体的生成表的脚本代码如下:
#coding=utf-8
import MySQLdb
import time
class smartHomeRobotDB:
def __init__(self,host,user,passwd,database):
self.db=MySQLdb.connect(host,user,passwd,database)
self.cur = conn.cursor()
self.initTable()
#清理数据库,系统初始化的时候调用
def removeAllTable(self):
self.cur.execute("drop table sensorDataRecord")
self.cur.execute("drop table userInfo")
self.cur.execute("drop table sysDeviceRealTimeStatus")
self.cur.execute("drop table rasterStatusRecord")
self.db.commit()
#创建smartHomeRobotDB系统数据表,通过该函数初始化创建膜拜单车系统数据库
def initTable(self):
try:
#create userInfo tablei
print("start create userInfo table")
self.cur.execute("create table userInfo(userID int(12) primary key not null auto_increment,
userName varchar(250) not null,
userSex varchar(4),
userAge int default null,
MD5 varchar(128) not null,
phone varchar(20) default null,
email nvarchar(250) default null,
lastLogin datetime default null,
residentID varchar(15) default null,
currentStatus int default null,
currentIP varchar(20) default null,
lastIMEI varchar(15) default null,
currentBalance float default null,
currentLocation point default null,
lastUpadateTime datetime default null,
lastLoginIP nvarchar(50) default null)"
)
#create sensorDataRecord table
#********************sensorDataRecord table**********************#
#
#
print("start create sensorDataRecord table")
self.cur.execute("create table sensorDataRecord(getTime datetime,
getLocation varchar(25),
mq2Value short,
lightValue int,
pm25Value1 float,
pm25Value2 float,
tempValue float,
humValue float,
ultrValue float,
sensorValueStatus short)"
)
#create sysDeviceRealTimeStatus table
#*******************sysDeviceRealTimeStatus table******************************#
#
#
print("start create sysDeviceRealTimeStatus table")
self.cur.execute("create table sysDeviceRealTimeStatus(ID int primary key not null auto_increment,
curSysStatus int ,
mq2SensorStatus short,
fanStatus short,
lightSensorStatus int,
buzzerStatus short,
pm25Sensor1Status float,
pm25Sensor2Status float,
tempSensorStatus float,
humSensorStatus float,
uartHMIStatus short,
robotStatus short ,
curLocation varchar(25),
targetLocation varchar(25),
guideLocation1 varchar(25),
guideLocation2 varchar(25),
guideLocation3 varchar(25),
guideLocation4 varchar(25),
guideLocation5 varchar(25),
guideLocation6 varchar(25),
tradingStatus int not null default 0)"
)
#create rasterStatusRecord table
#************************rasterStatusRecord table*************************************#
#
#
print("start create rasterStatusRecord table")
self.cur.execute("create table rasterStatusRecord(rasterLocation varchar(25),
rasterLocationX float,
rasterLocationY float,
rasterStatus short)"
)
print("create all sucessful")
except:
print("table is already create")
通过运行上述脚本,就可以在系统中构建smartHomeRobotDB数据库系统,为系统存储和访问数据提供支持,本文采用的是mySQL数据库作为底层的数据库支持,通过提供的python接口可以方便的实现对数据的访问。
|