python - BHV1750vi 数据库输入为 "Not all parameters were used in the SQL statement"

标签 python mysql

我在以下 python 代码中遇到错误。我在树莓派上执行此代码,我想从 BHV1750vi(光发射传感器)获取数据库(MySQL)的输入。

import smbus
import time
from mysql.connector import MySQLConnection, Error
from test_dbconfig import read_db_config

# Define some constants from the datasheet

DEVICE     = 0x23 # Default device I2C address

POWER_DOWN = 0x00 # No active state
POWER_ON   = 0x01 # Power on
RESET      = 0x07 # Reset data register value

# Start measurement at 4lx resolution. Time typically 16ms.
CONTINUOUS_LOW_RES_MODE = 0x13
# Start measurement at 1lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
# Start measurement at 0.5lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_1 = 0x20
# Start measurement at 0.5lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_2 = 0x21
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_LOW_RES_MODE = 0x23

#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(1)  # Rev 2 Pi uses 1

def convertToNumber(data):
# Simple function to convert 2 bytes of data
  # into a decimal number
  return ((data[1] + (256 * data[0])) / 1.2)

def readLight(addr=DEVICE):
  data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
  return convertToNumber(data)

def insert_lightlux(l):
    query = "INSERT INTO light(lux) VALUES(%s)"
    args = (l)

    try:
        db_config = read_db_config()
        conn = MySQLConnection(**db_config)

        cursor = conn.cursor()
        cursor.executemany(query, args)
        if cursor.lastrowid:
            print('last insert id', cursor.lastrowid)
        else:
            print('last insert id not found')

        conn.commit()
    except Error as error:
        print(error)

    finally:
        cursor.close()
        conn.close()

def main():
  while True:
      if readLight() is not None:
       print "luminous emmittance  " + str(readLight()) + " lx"
     time.sleep(10)
       l = [(str(readLight()))]
       insert_lightlux(l)
      else:
       print('Failed to get reading. Try again!')
       sys.exit(1)

if __name__=="__main__":
   main()

错误信息是

luminous emmittance  9.16666666667 lx
Not all parameters were used in the SQL statement

有人知道我的代码有什么问题吗?

最佳答案

问题在于如何为 executemany 构建 args,它需要一个元组列表,而您有一个列表元组。

换句话说,你的参数是 ([(value)]) 但它应该是 [(value)]

尝试将 args = (l) 更改为

args = l

关于python - BHV1750vi 数据库输入为 "Not all parameters were used in the SQL statement",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952983/

相关文章:

python - 在 scipy 中计算稀疏矩阵的列之间的相关性时如何忽略零

python - 如何使用setuptools安装到自定义目录?

php - 如何以网络表单上传文件并通过电子邮件发送?

mysql - select * from tablename where id ='' 和 select * from tablename where id is null 之间的区别

mysql - 了解 MySQL log_queries_not_using_indexes 行为

mysql - 使用c连接到mysql数据库时出现段错误

python - 使用 Django 在 Elastic Beanstalk 上运行 Python 的 Celery

python - KeyError : 'text/plain' mean? 是什么意思

python - 将 PyOpenGL 从 PyGame 转移到 PyQt5 的困惑

php - 使用PHP获取一行中的所有数据而不重复