简体   繁体   中英

Adding Columns to MySQL results in Column Count error

I'm brand new and self taught, so please excuse my less than pythonic code, general jankiness, exposure to mysql injection and no error catching but so far ive been able to write a little python program that extracts some key data from a JSON response from a URL and pipes it into a mysql (mariadb) database, then grafana picks up the rows and prettifies the data.

#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']

I was using the above 10 variables and placing those into a mysql table with:

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()

It was all working beautifully and then I just wanted to add one more variable "dewpoint" (its a weather station). I hopped into phpmyadmin, open up the weather database and fire an extra column onto the end called deltadew . Using this horrific mess i make that calculated variable.

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

So now all I need to do (in my mind) is add another %s to the VALUES and feed it the variable deltadew . But i'm met with the console error "Column count doesn't match value count at row 1" Which, following some googling and searching on here (none answered the 1+1=error scenario) seems like i'm not providing enough (or too many) arguments for the number of columns available.

Please help.

The solution was that I had added the additional variable to the MySQL statement in VALUES but not into INSERT INTO . We live and we learn.

For those morbidly curious; this is the full program. Doesn't matter about the credentials as it only runs locally to me and the URL is publicly available information.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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