1

最初にテーブルを削除せずにデータベースにデータを挿入するPythonスクリプトを取得しようとしています..これを行うのは難しくないと確信していますが、コードを正しく取得できないようです..完全な python スクリプト..

#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import hashlib
import time
import MySQLdb


#Dont forget to fill in PASSWORD and URL TO saveTemp (twice) in this file

sensorids = ["28-000004944b63", "28-000004c01b2c"] 
avgtemperatures = []
for sensor in range(len(sensorids)):
        temperatures = []
        for polltime in range(0,3):
                text = '';
                while text.split("\n")[0].find("YES") == -1:
                        # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
                        tfile = open("/sys/bus/w1/devices/"+ sensorids[sensor] +"/w1_slave")
                        # Read all of the text in the file.
                        text = tfile.read()
                        # Close the file now that the text has been read.
                        tfile.close()
                        time.sleep(1)

                # Split the text with new lines (\n) and select the second line.
                secondline = text.split("\n")[1]
                # Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
                temperaturedata = secondline.split(" ")[9]
                # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
                temperature = float(temperaturedata[2:])
                # Put the decimal point in the right place and display it.
                temperatures.append(temperature / 1000 * 9.0 / 5.0 + 32.0)

        avgtemperatures.append(sum(temperatures) / float(len(temperatures)))

print avgtemperatures[0]
print avgtemperatures[1]

 #connect to db
db = MySQLdb.connect("localhost","user","password","temps" )

 #setup cursor
cursor = db.cursor()

 #create temps table
cursor.execute("DROP TABLE IF EXISTS temps")


sql = """CREATE TABLE temps (
      temp1 FLOAT,  
      temp2 FLOAT )"""
cursor.execute(sql)


 #insert to table
try:
    cursor.execute("""INSERT INTO temps VALUES (%s,%s)""",(avgtemperatures[0],avgtemperatures[1]))
    db.commit()
except:     
    db.rollback()


 #show table
cursor.execute("""SELECT * FROM temps;""")

print cursor.fetchall()
((188L, 90L),)

db.close()

これは私が助けを必要としている部分です..

テーブルを削除しても問題なく動作しますが、テーブルを削除したくないので、新しいデータを同じテーブルに挿入するだけです。

 #connect to db
db = MySQLdb.connect("localhost","user","pasword1","temps" )

 #setup cursor
cursor = db.cursor()

 #create temps table
cursor.execute("DROP TABLE IF EXISTS temps")


sql = """CREATE TABLE temps (
      temp1 FLOAT,  
      temp2 FLOAT )"""
cursor.execute(sql)


 #insert to table
try:
    cursor.execute("""INSERT INTO temps VALUES (%s,%s)""",(avgtemperatures[0],avgtemperatures[1]))
    db.commit()
except:     
    db.rollback()


 #show table
cursor.execute("""SELECT * FROM temps;""")

print cursor.fetchall()
((188L, 90L),)

db.close()
4

2 に答える 2