gpt4 book ai didi

mysql - Python Mariadb MYSQL 类型转换错误 不正确的双值 : '' 23 7'' for column `wtx520` .`weather` .`value` at row 1

转载 作者:行者123 更新时间:2023-11-29 15:43:22 24 4
gpt4 key购买 nike

我有一个 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):不正确的 double 值:第 1 行的 wtx520.weather.value 列为“237” 奎勒= 斯塔佩鲁贝尔瓦赫: 文件“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 Mariadb MYSQL 类型转换错误 不正确的双值 : '' 23 7'' for column `wtx520` .`weather` .`value` at row 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57339317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com