python - Insert data instead of drop table into mysql -


i'm attempting python script insert data database without having drop table first.. i'm sure isn't hard can't seem code right.. here full python script..

#!/usr/bin/python # -*- coding: utf-8 -*- import requests import hashlib import time import mysqldb   #dont forget fill in password , url savetemp (twice) in file  sensorids = ["28-000004944b63", "28-000004c01b2c"]  avgtemperatures = [] sensor in range(len(sensorids)):         temperatures = []         polltime in range(0,3):                 text = '';                 while text.split("\n")[0].find("yes") == -1:                         # open file viewed earlier python can see in it. replace serial number before.                         tfile = open("/sys/bus/w1/devices/"+ sensorids[sensor] +"/w1_slave")                         # read of text in file.                         text = tfile.read()                         # close file text has been read.                         tfile.close()                         time.sleep(1)                  # split text new lines (\n) , select second line.                 secondline = text.split("\n")[1]                 # split line words, referring spaces, , select 10th word (counting 0).                 temperaturedata = secondline.split(" ")[9]                 # first 2 characters "t=", rid of , convert temperature string number.                 temperature = float(temperaturedata[2:])                 # put decimal point in right place , 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 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 table try:     cursor.execute("""insert temps values (%s,%s)""",(avgtemperatures[0],avgtemperatures[1]))     db.commit() except:          db.rollback()    #show table cursor.execute("""select * temps;""")  print cursor.fetchall() ((188l, 90l),)  db.close() 

this part need assistance with..

if have drop table works fine don't want drop table, insert new data same table.

 #connect 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 table try:     cursor.execute("""insert temps values (%s,%s)""",(avgtemperatures[0],avgtemperatures[1]))     db.commit() except:          db.rollback()    #show table cursor.execute("""select * temps;""")  print cursor.fetchall() ((188l, 90l),)  db.close() 

you shouldn`t have drop table each time want enter data. in fact, defeats whole purpose of database since remove previous data each time run script.

you should ask create table if not exists. use following.

sql = """create table if not exists temps (   temp1 float,     temp2 float )""" cursor.execute(sql) 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -