我有一个Raspi Python项目,我想在其中将天气传感器的值存储在数据库中。值以字符串形式出现。我能够将其分离,但是我犯了一个愚蠢的错误,无法存储值。
码
mport mysql.connector
import re
db =mysql.connector.connect(host='localhost', port=3306, db='wtx520', password='xxx', user='xxx')
#port 3307 für synology
#example string
line ="0r1,Dn=237D,Dm=237D,Dx=237D,Sn=0.1N,Sm=0.2N,Sx=0.3NLq@"
pair = line.split(",")
for i in pair:
value = i.split("=")
print(value[0])
if len(value)>1:
decvalue =re.findall("\d*\.?\d+", value[1])
key= value[0]
#remove brackets around dec value
inserttuple=(key,str(decvalue).replace('[','').replace(']',''))
#inserttuple=(str(key), "237") it works with this static example
sql_insert_query = """INSERT INTO weather (valuekey, value) VALUES (%s,%s)"""
mycursor = db.cursor()
result = mycursor.execute(sql_insert_query,inserttuple)
db.commit()
数据库表定义:
CREATE TABLE wtx520.weather (
ts TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
value DOUBLE DEFAULT NULL,
valuekey VARCHAR(4) DEFAULT NULL
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 1365,
CHARACTER SET utf8,
COLLATE utf8_general_ci;
错误:
Nachricht = 1366(22007):错误的双精度值:第1行的
wtx520
。weather
。value
列的``237''奎尔=
Stapelüberwachung:
文件“ C:\ Users \ Marcel \ source \ repos \ wtx520-azure \ wtx520-azure \ wtx520_azure.py”,第22行,在
结果= mycursor.execute(sql_insert_query,inserttuple)
任何帮助
最佳答案
解决方法是:
inserttuple =(key,str(decvalue).replace('[','')。replace(']','')。replace(“'”,''))
感谢您的提示!
关于mysql - Python Maria DB MYSQL类型强制转换错误不正确的 double 值:第1行的“wtx520” .``weather..value”列的“237”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57339317/