資源簡介 #SQLite數據庫基本操作import sqlite3#連接/創建數據庫conn = sqlite3.connect("Sqlite/text.db")#創建游標cu = conn.cursor()#創建數據表(表名st)#create table <表名>(<字段名> <字段數據類型>,……)cu.execute('''create table st( ID int primary key not NULL, name text not NULL, age int not NULL, sex char(1) not NULL)''')conn.commit() #保存并提交到數據庫#向數據庫增加數據#insert into <表名> (字段1,字段2,……) valuse(值1,值2,……)cu.execute("insert into st(ID,name,age,sex) values(1,'Peter',15,'M')")cu.execute("insert into st(ID,name,age,sex) values(2,'Jim',16,'M')")cu.execute("insert into st(ID,name,age,sex) values(3,'Maray',14,'W')")cu.execute("insert into st(ID,name,age,sex) values(4,'Brent',17,'M')")conn.commit() #提交數據#查找#select <輸出列1>,<輸出列2> form <表名> where <篩選條件>s = cu.execute("select * from st")for i in s: print(i)s = cu.execute("select name,age from st")for i in s: print(i)s = cu.execute("select name,age from st where age>15")for i in s: print(i)#用fetchall() 獲取游標結果,返回的是列表類型cu.execute("select name,age from st where age>=15")print(cu.fetchall())#刪除語句#delete from <表名> where <篩選條件>cu.execute("delete from st where ID=1 ")conn.commit() #更新cu.close()conn.close()#室內環境檢測系統flask代碼import sqlite3 #導入 sqlite3 數據庫import jsonfrom flask import Flask,render_template, requestDATABASE = "./flask_class4/data/data.db" #定義數據庫路徑app = Flask(__name__) #創建一個服務器實例@app.route("/")def hello(): db = sqlite3.connect(DATABASE) #連接數據庫 data.db cur = db.cursor() #創建游標對象 cur.execute("SELECT * FROM sensorlog WHERE sensorid =1") #Execute方法用來執行sql語句,當sensorid=1, 從"sensorlog" 表中選取所在列 data = cur.fetchall() #查詢所有數據 cur.close() #關閉游標,不自動提交保存。 db.close() #關閉數據庫,不自動提交保存。 temp1 = data[len(data) - 1] #獲取最新一行的數據,[ID,TIME,TEMP] temp = temp1[2] #獲取溫度 return render_template('vews1.html', data=data,temp=temp)#Get data@app.route("/get",methods=['GET']) #響應網頁的 get 請求,返回實時溫度def get_data(): sensorid=int(request.args.get('id')) #獲取id值db = sqlite3.connect(DATABASE) #連接數據庫 data.dbcur = db.cursor() #創建游標對象 cur.execute("SELECT * FROM sensorlog WHERE sensorid = %s"% sensorid)#Execute方法用來執行sql語句,當sensorid=1, 從"sensorlog" 表中選取所在列 data = cur.fetchall() #查詢所有數據 dbsum=len(data) #所有數據的長度 dset={'sensorid':sensorid} #字典 temp=[] #空數組 for i in range(dbsum): #在該數據長度下輪詢 value={} #空的字典 value['sensorvalue']=data[i][2] #當前i值的溫度 value['updatetime']=data[i][3] #當前i值的時間 temp.append(value) #在temp對象末尾創建value值 #dset['value']={'sensorvalue':data[i-1][2],'updatetime':data[i-1][3]} dset['value']=temp #在該字典后面放入溫度和時間 djson=json.dumps(dset)#把編碼字典數據 return djson #返回所有數據#Adding data@app.route("/input",methods=['POST','GET']) #響應終端的 post和get 請求,獲取實時溫度def add_data(): if request.method == 'POST': sensorid = int(request.form.get('id')) sensorvalue = float(request.form.get('val')) else: sensorid = int(request.args.get('id')) sensorvalue = float(request.args.get('val')) nowtime = datetime.datetime.now() nowtime = nowtime.strftime('%Y-%m-%d %H:%M:%S') db = sqlite3.connect(DATABASE) cur = db.cursor()cur.execute("INSERT INTO sensorlog(sensorid,sensorvalue,updatetime) VALUES(%d,%f,'%s')" %(sensorid,sensorvalue,nowtime) ) mit() #事務提交,保存修改內容。 cur.execute("SELECT * FROM sensorlist where sensorid = %d"% sensorid) rv = cur.fetchall() cur.close() db.close() #關閉數據庫,不自動提交保存。 maxrv = rv[0][2] minrv = rv[0][3] if sensorvalue > maxrv or sensorvalue < minrv: return '1' else: return '0'if __name__ == "__main__": app.run(host="0.0.0.0", port=8080,threaded=True) #綁定 Web 服務器的 IP 和端口#室內環境檢測系統html代碼室內環境監測系統室內環境監測系統當前室內溫度:`temp` 刷新歷史數據列表: 溫度 記錄時間 {% for i in data[::-1] %} {{i[2]}} {{i[3]}} {% endfor %}提交數據:/input id=1&val=255獲取數據:/get id=1 展開更多...... 收起↑ 資源預覽 縮略圖、資源來源于二一教育資源庫