あーあ、なんとか元通り。そのまま書きます。。。。
==========================
20170205_pytosql6.py
==========================
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# import module
import os
import random # モジュールのインポート
import os
import random # モジュールのインポート
# ドライバをimport
import mysql.connector
import mysql.connector
if __name__ == ‘__main__’:
# データベースに接続
# データベースに接続
# localhost access ok!!!!!
connect = mysql.connector.connect(user=’***t’, password=’***t’, host=’********t’, database=’201****_s***or’, charset=’utf8′)
connect = mysql.connector.connect(user=’***t’, password=’***t’, host=’********t’, database=’201****_s***or’, charset=’utf8′)
cursor = connect.cursor()
# sakuravps access
# connect1 = mysql.connector.connect(user=’r****i’, password=’A******6′, host=’1*0.16.**.1*7′, database=’2****129_s***or’, charset=’utf8′)
# connect1 = mysql.connector.connect(user=’r****i’, password=’A******6′, host=’1*0.16.**.1*7′, database=’2****129_s***or’, charset=’utf8′)
# cursor = connect1.cursor()
temp=random.randint(0, 50)
humid=random.randint(0, 100)
humid=random.randint(0, 100)
# insert
# cursor.execute(‘insert into t_sensor (time_stamp,temp,humid,water_need,memo) values (now(), %s, %s, %s, %s)’, (100,100,’20170205test’,’test_success_2017/02/05′))
# cursor.execute(‘insert into t_sensor (time_stamp,temp,humid,water_need,memo) values (now(), %s, %s, %s, %s)’, (100,100,’20170205test’,’test_success_2017/02/05′))
cursor.execute(‘insert into t_sensor (time_stamp,temp,humid,water_need,memo) values (now(), %s, %s, %s, %s)’, (temp,humid,’2017/02/06_test’,’test_success_2017/02/06′))
# Delete
cursor.execute(‘DELETE FROM t_sensor ORDER BY Num ASC LIMIT 1’)
cursor.execute(‘DELETE FROM t_sensor ORDER BY Num ASC LIMIT 1’)
# select
cursor.execute(‘select * from t_sensor order by Num DESC limit 2’)
cursor.execute(‘select * from t_sensor order by Num DESC limit 2’)
rows = cursor.fetchall()
# 出力
for i in rows:
print(“————————“)
print(i[0])
print(i[1])
print(i[2])
print(i[3])
print(i[4])
print(i[5])
for i in rows:
print(“————————“)
print(i[0])
print(i[1])
print(i[2])
print(i[3])
print(i[4])
print(i[5])
# autocommitではないので、明示的にコミットする
connect.commit()
connect.commit()
# データベースから切断
cursor.close()
connect.close()
cursor.close()
connect.close()
==========================
20170206_sndsak2.py
==========================
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
# import module
import os
import random # モジュールのインポート
import os
import random # モジュールのインポート
# ドライバをimport
import mysql.connector
import mysql.connector
if __name__ == ‘__main__’:
# データベースに接続
# データベースに接続
# localhost access ok!!!!!
connect = mysql.connector.connect(user=’***t’, password=’***t’, host=’********t’, database=’20******5_s****r’, charset=’utf8′)
connect = mysql.connector.connect(user=’***t’, password=’***t’, host=’********t’, database=’20******5_s****r’, charset=’utf8′)
cursor = connect.cursor()
# select
cursor.execute(‘select * from t_sensor order by Num DESC limit 1’)
cursor.execute(‘select * from t_sensor order by Num DESC limit 1’)
rows = cursor.fetchall()
# 出力
for i in rows:
print(“—send below data to sakravps success !!—-“)
print(i[0])
print(i[1])
print(i[2])
print(i[3])
print(i[4])
print(i[5])
for i in rows:
print(“—send below data to sakravps success !!—-“)
print(i[0])
print(i[1])
print(i[2])
print(i[3])
print(i[4])
print(i[5])
# sakuravps access
connect = mysql.connector.connect(user=’r***ai’, password=’A****456′, host=’1*0.**.**.1*7′, database=’20*****9_****or’, charset=’utf8′)
connect = mysql.connector.connect(user=’r***ai’, password=’A****456′, host=’1*0.**.**.1*7′, database=’20*****9_****or’, charset=’utf8′)
cursor = connect.cursor()
# insert
cursor.execute(‘insert into t_sensor (time_stamp,temp,humid,water_need,memo) values (%s, %s, %s, %s, %s)’, (i[1],i[2],i[3],i[4],i[5]))
# Delete
cursor.execute(‘DELETE FROM t_sensor ORDER BY Num ASC LIMIT 1’)
cursor.execute(‘DELETE FROM t_sensor ORDER BY Num ASC LIMIT 1’)
# autocommitではないので、明示的にコミットする
connect.commit()
connect.commit()
# データベースから切断
cursor.close()
connect.close()
cursor.close()
connect.close()
==========================
ちなみにcrontabは下記です。
50 * * * * sudo python 20170205_pytosql6.py
51 * * * * sudo python 20170206_sndsak2.py
51 * * * * sudo python 20170206_sndsak2.py
==========================
The End