배열 MySQL insert

Posted by PeEn
2019. 11. 17. 17:37 Programing/Python
# -*- coding: utf-8 -*-
import json
import pymysql
import urllib.request


curtime = ['2019-11-17 17:04:37', '2019-11-17 17:01:38', '2019-11-17 16:58:39', '2019-11-17 16:55:41', '2019-11-17 16:52:42', '2019-11-17 16:49:43', '2019-11-17 16:46:44', '2019-11-17 16:43:45', '2019-11-17 16:40:45', '2019-11-17 16:37:47', '2019-11-17 16:34:47', '2019-11-17 16:31:48', '2019-11-17 16:28:50', '2019-11-17 16:25:51', '2019-11-17 16:22:53', '2019-11-17 16:19:54', '2019-11-17 16:16:54', '2019-11-17 16:13:55', '2019-11-17 16:10:57', '2019-11-17 16:07:57']
pm1_0_cf1 = ['14', '15', '13', '14', '13', '11', '11', '13', '11', '12', '12', '11', '12', '11', '13', '10', '12', '11', '10', '12']
pm2_5_cf1 = ['18', '18', '16', '17', '18', '13', '13', '17', '14', '14', '16', '14', '19', '14', '16', '13', '15', '13', '11', '14']
pm1_0_atm = ['14', '15', '13', '14', '13', '11', '11', '13', '11', '12', '12', '11', '12', '11', '13', '10', '12', '11', '10', '12']
pm2_5_atm = ['18', '18', '16', '17', '18', '13', '13', '17', '14', '14', '16', '14', '19', '14', '16', '13', '15', '13', '11', '14']
air_0_3 = ['2250', '2481', '2085', '2223', '2166', '1854', '1881', '2151', '1956', '2118', '1980', '1827', '2049', '2040', '2193', '2028', '1947', '1848', '1701', '1896']
air_0_5 = ['675', '736', '616', '663', '655', '549', '554', '637', '573', '613', '586', '561', '614', '599', '665', '606', '578', '543', '512', '575']
air_1_0 = ['112', '96', '83', '100', '98', '74', '70', '82', '58', '61', '86', '84', '117', '82', '77', '66', '82', '57', '44', '73']
air_2_5 = ['4', '4', '4', '6', '8', '2', '4', '6', '6', '0', '14', '6', '12', '6', '4', '4', '4', '6', '0', '2']

range(0,19)

# thingspeak
api_key = '0BNMXAL2AURZGJB8'
url = 'https://api.thingspeak.com/update'

# db
conn = pymysql.connect(host='localhost', port = 3306,user = '아이디', passwd = '비번')
sql = "INSERT INTO sci.sensor(`curtime`,`pm1_0_cf1`,`pm2_5_cf1`,`pm1_0_atm`,`pm2_5_atm`,`air_0_3`,`air_0_5`,`air_1_0`,`air_2_5`) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s);"
for i in range(19):
    print(curtime[i])
    val = curtime[i],str(pm1_0_cf1[i]),str(pm2_5_cf1[i]),str(pm1_0_atm[i]),str(pm2_5_atm[i]),str(air_0_3[i]),str(air_0_5[i]),str(air_1_0[i]),str(air_2_5[i])
    cur = conn.cursor()
    cur.execute(sql,val)
    conn.commit()


cur.close()
conn.close()


'Programing > Python' 카테고리의 다른 글

ThingSpeak insert(차트 자동으로 그려주는 클라우드)  (0) 2019.11.17
컴프리핸션 Comprehension  (0) 2019.05.27
출력  (0) 2019.05.27
문자열  (0) 2019.05.27
랜덤 난수  (0) 2019.05.27