
==================================
1.進入phpmyadmin並登入
2.建立資料庫 mydb
*utf8_general_ci
3.建立資料表 ds18b20
*欄位: 流水號(INT,自動遞增,主鍵)、時戳(TIMESTAMP)、溫度(FLOAT)
4.安裝MySQL在Python環境下使用套件
sudo apt-get install python-mysqldb
5.編寫Python程式 ds18b20.py
*引入資料庫函式庫 import MySQLdb
*連線 db= MySQLdb.connect(host="localhost",user="root",passwd="12345679",db="mydb")
*對應資料表
cursor = db.cursor()
*插入新資料
格式: INSERT INTO 資料表 (欄位) VALUES (值)
sql_str="INSERT INTO ds18b20 (rec_temp) VALUES ("+str(temp_now)+")"
*執行
cursor.execute(sql_str)
*真正寫入
db.commit()
*關閉資料庫
db.close()
6.時區若錯誤,需要設定
sudo dpkg-reconfigure tzdata
*選擇Taipei時區
*重開機 sudo reboot
7.改良程式--中斷
try...except KeyboardInterrupt:
==================================