Click here to Skip to main content
14,975,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import sys
import os
import time
import calendar
#import MariaDB
#import phpMyAdmin
import MySQLdb
#import pymysql
import time
from time import *

lt = localtime()

file = open('/sys/bus/w1/devices/28-030797798738/w1_slave')
filecontent =
stringvalue = filecontent.split("\n")[1].split(" ")[9]
temperatur_sensor1 = float(stringvalue[2:]) / 1000
print "hhhhhhh", temperatur_sensor1
connection = MySQLdb.connect (host="localhost", user="Smart", passwd="123456", db="mysql", charset='utf8')

cursor = connection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS temperatur6 (temperatur6 FLOAT(5))""")
print "ggggggg", temperatur_sensor1
cursor.execute("INSERT INTO temperatur6 VALUES ( '%s' ) "),(temperatur_sensor1)
print "ffffff", temperatur_sensor1



The Waring is?___________________________________________________________ Warning: Incorrect double value: '%s' for column 'temperatur6' at row 1
  cursor.execute("INSERT INTO temperatur6 VALUES ( '%s' ) "),(temperatur_sensor1)

What I have tried:

I tried almost everythin already. Inset a %d or to delete the '' signs.
Updated 10-Jun-19 0:04am
[no name] 9-Jun-19 10:13am
Obsolete after Q EditYour table has one column 'temperatur6', but you try to insert two columns...

temperatur_sensor1 is a float value, not a string.
Member 14488635 9-Jun-19 11:12am
Da soll aber eine Dezimalzahl wie z.Bsp: 29.44 rein. Was muss ich denn ändern? Danke!!
[no name] 9-Jun-19 12:03pm
Du solltest in Englisch antworten ;)
Richard MacCutchan 10-Jun-19 3:37am
This is an English language site. If you do not speak reasonable English you can use Google Translate.
Member 14488635 10-Jun-19 5:28am
Sorry. I forgot to write in English.
I just want to know what I have to change in the code program? I am really glueless. Can you give me a hint? Please!
Richard MacCutchan 10-Jun-19 5:36am
Member 14488635 10-Jun-19 5:39am
Ok. This might be the best. Thank you for your help. Let you know if it was successful. Thankx
Member 14488635 10-Jun-19 6:02am
The print works fine!
Richard MacCutchan 10-Jun-19 6:04am
See my suggestion below.
Member 14488635 10-Jun-19 6:24am
This is the result!
hhhhhhh 20.125 Warning: Table 'temperatur6' already exists
cursor.execute("""CREATE TABLE IF NOT EXISTS temperatur6 (temperatur6 float(5))""")
ggggggg 20.125
Traceback (most recent call last):
File "", line 37, in <module>
cursor.execute("""INSERT INTO temperatur6 VALUES (%s)""", (temperatur_sensor1))
File "/usr/lib/python2.7/dist-packages/MySQLdb/", line 207, in execute
args = tuple(map(db.literal, args))
TypeError: argument 2 to map() must support iteration

But it looks better. At least your suggestion was right! Thankx
Richard MacCutchan 10-Jun-19 6:50am
OK, try without the parentheses round the value:
cursor.execute("""INSERT INTO temperatur6 VALUES (%s) """, temperatur_sensor1)
Richard MacCutchan 10-Jun-19 7:53am
My final guess is that the value field needs a comma at the end if there is only one item:
cursor.execute("""INSERT INTO temperatur6 VALUES (%s) """, (temperatur_sensor1,))
This is a guess as I do not have MySqldb on my system. But looking at the syntax of MySQL statements in the documentation I wonder if your quote characters and parentheses are correct. Try changing the INSERT to:
cursor.execute("""INSERT INTO temperatur6 VALUES (%s) """,(temperatur_sensor1))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900