我在以下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
有谁知道我的代码有什么问题吗?
最佳答案
问题在于如何为args
构建executemany
,当您拥有一个列表元组时,它会期待一个元组列表。
换句话说,您的arg为([(value)])
,但应为[(value)]
尝试将args = (l)
更改为
args = l
关于python - BHV1750vi数据库输入上的“SQL语句中未使用所有参数”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36952983/