繁体   English   中英

向 MySQL 添加列会导致列计数错误

[英]Adding Columns to MySQL results in Column Count error

我是全新的,自学成才,所以请原谅我的 Python 代码,一般的 jankiness,暴露于 mysql 注入并且没有捕获错误,但到目前为止,我能够编写一个小的 python 程序,从 Z0ECD20FD11C1D7A2A2878 响应中提取一些关键数据从 URL 并将其通过管道传输到 mysql (mariadb) 数据库中,然后 grafana 拾取行并美化数据。

#get varibles from JSON dict
humidity = int(weather['data'][str(siteid)][humi]['v'])
airtemp = float(weather['data'][str(siteid)][air]['v'])
windgusts = int(weather['data'][str(siteid)][windg]['v'])
windspeed = int(weather['data'][str(siteid)][winds]['v'])
windchill = float(weather['data'][str(siteid)][windc]['v'])
dewpoint = float(weather['data'][str(siteid)][dp]['v'])
winddirection = int(weather['data'][str(siteid)][winddir]['v'])
pressure = int(weather['data'][str(siteid)][prs]['v'])
date_got = weather['sites'][0]['datatime']
site = weather['sites'][0]['sitename']

我正在使用上述 10 个变量并将它们放入 mysql 表中:

addtodata.execute("INSERT INTO weather (humidity, airtemp, windgusts, windspeed, windchill, \
        dewpoint, windirection, pressure, date_got, site) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",\
            (humidity, airtemp, windgusts, windspeed, windchill, dewpoint, winddirection, pressure, current_time, site))
        db.commit()

一切都很顺利,然后我只想添加一个可变的“露点”(它是一个气象站)。 我跳进 phpmyadmin,打开weather数据库并在末尾添加一个名为deltadew的额外列。 使用这种可怕的混乱,我制作了计算变量。

#initiate fudge factor
steel1 = (airtemp / windchill)*0.4
steeltemp = airtemp - steel1
delta1 = steeltemp - dewpoint
deltadew = delta1

所以现在我需要做的(在我看来)就是在VALUES中添加另一个%s并将其提供给变量deltadew 但是我遇到了控制台错误"Column count doesn't match value count at row 1" ,在此处进行一些谷歌搜索和搜索之后(没有回答 1+1=error 场景)似乎我没有提供足够的(或太多)arguments 可用的列数。

请帮忙。

解决方案是我已将附加变量添加到VALUES中的 MySQL 语句中,但没有添加到INSERT INTO中。 我们生活,我们学习。

对于那些病态好奇的人; 这是完整的程序。 凭证无关紧要,因为它只在我本地运行,并且 URL 是公开可用的信息。

import requests
import json
import schedule
import time
import logging
import mysql.connector as sql

db = sql.connect(
    host="192.168.0.106",
    user="weather",
    passwd="Y73si9VB",
    database="weather")

addtodata = db.cursor()

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                     level=logging.DEBUG)

#constants
siteid=1063
url=f"http://www.mmweather.net/data/current-data?paramnames=s1pwcode%2Cs1heading1%2Cs1winddir2%2Cs1windspeed2%2Cs1windbacked2%2Cs1windveered2%2Cs1winddir2%2Cs1windspeed2%2Cs1windgust2%2Cs1winddir10%2Cs1windspeed10%2Cs1windgust10%2Cs1qnh10%2Cs1qfe10%2Cs1tempdry10%2Cs1dewpoint10%2Cs1humidity10%2Cs1windchill10%2Cs1cloudbase1%2Cs1cloudbase2%2Cs1visibility10%2Cs1winddir10%2Cs1windspeed10%2Cs1windgust10%2Cs1qnh10%2Cs1tempdry10%2Cs1humidity10%2Cs1windchill10%2Cs1cloudbase1%2Cs1visibility10%2Cs1tshs%2Cs1sptp&siteids={siteid}&metar=1"

def send_weather():
    #get the current time for timestamping to the record
    t = time.localtime()
    current_time = time.strftime("%y-%m-%d %H:%M:%S", t)
    try:
        response = requests.get(url)
        weather = json.loads(response.text)
        #for testing only when the below is commented out
        #print(json.dumps(weather, indent=4, sort_keys=True))
    except:
        print("I didn't get any JSON data that time."
            f"My last attempt was at {current_time}")
    else:
        humi="87"
        air="61"
        windg="15"
        winds="14"
        windc ="84"
        dp="81"
        winddir="13"
        prs="96"

        #get varibles from JSON dict
        humidity = int(weather['data'][str(siteid)][humi]['v'])
        airtemp = float(weather['data'][str(siteid)][air]['v'])
        windgusts = int(weather['data'][str(siteid)][windg]['v'])
        windspeed = int(weather['data'][str(siteid)][winds]['v'])
        windchill = float(weather['data'][str(siteid)][windc]['v'])
        dewpoint = float(weather['data'][str(siteid)][dp]['v'])
        winddirection = int(weather['data'][str(siteid)][winddir]['v'])
        pressure = int(weather['data'][str(siteid)][prs]['v'])
        date_got = weather['sites'][0]['datatime']
        site = weather['sites'][0]['sitename']
        #initiate fudge factor
        steel1 = (airtemp / windchill)*0.4
        steeltemp = airtemp - steel1
        delta1 = steeltemp - dewpoint
        deltadew = delta1

        #whack the found variables to the console
        print(f"Humidity:{humidity}, Air Temp:{airtemp}, Gusts:{windgusts}, Wind Speed:{windspeed},"
            f"\nwindchill:{windchill}, Dewpoint:{dewpoint}, Wind Direction:{winddirection},"
            f"\nPressure: {pressure}, Date:{date_got}, Site:{site}")

        #add the retrieved values to the MariaDB
        addtodata.execute("INSERT INTO weather (humidity, airtemp, windgusts, windspeed, windchill, \
            dewpoint, windirection, pressure, date_got, site, deltadew) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",\
            (humidity, airtemp, windgusts, windspeed, windchill, dewpoint, winddirection, pressure, current_time, site, deltadew))
        db.commit()
    #for testing only when the below is commented out
    #send_weather()

        gobaby = (f"This data was retrieved from the server on {date_got} \n"
            f"from the {site}\n\n"
            f"The Air Temperature is {airtemp}C. Dewpoint is {dewpoint}C\n"
            f"The Humidity is {humidity}% RH\n"
            f"Windspeed is {windspeed} knots.\n"
            f"and it's gusting at {windgusts} knots.\n\n"
            "*****************************\n")
        with open('weatheroutput.txt', 'a') as f:
            f.writelines(gobaby)

#schedule.every().hour.at(":03").do(send_weather)
schedule.every(15).minutes.do(send_weather)
t = time.localtime()
current_time = time.strftime("%y-%m-%d %H:%M:%S", t)
print(f"Running!\n"
    f"The time I started is {current_time}")
while True:
    schedule.run_pending()
    time.sleep(30)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM