」上的SQL語句中並非所有參數都使用「我在以下python代碼中遇到錯誤。我在樹莓派上執行此代碼,並且我想從BHV1750vi(光發射度傳感器)獲取數據庫(MySQL)的輸入。「在BHV1750vi數據庫輸入
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
有沒有人知道什麼是錯在我的代碼?
的[並非所有參數都在SQL語句(Python中,MySQL的)使用(可能的複製http://stackoverflow.com/questions/20818155/not-all-parameters-were-used-in-the-sql-statement-python-mysql) –
我已經按照那個問題的任何答案,不會很好結果@AniMenon – Thebrizkys
這與你提問無關,但我真的需要告訴你:你def首先應該將readLight()緩存在主循環中的一個變量中 – Francesco