2016-04-30 33 views
0

」上的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 

有沒有人知道什麼是錯在我的代碼?

+0

的[並非所有參數都在SQL語句(Python中,MySQL的)使用(可能的複製http://stackoverflow.com/questions/20818155/not-all-parameters-were-used-in-the-sql-statement-python-mysql) –

+0

我已經按照那個問題的任何答案,不會很好結果@AniMenon – Thebrizkys

+0

這與你提問無關,但我真的需要告訴你:你def首先應該將readLight()緩存在主循環中的一個變量中 – Francesco

回答

0

問題是你如何爲executemany構建args,當你有一個列表元組時,它需要一個元組列表。

換句話說,你的arg是([(value)])但它應該是[(value)]

嘗試改變args = (l)

args = l 
+0

仍然得到「並非所有參數都在SQL語句中使用」消息。我對此很好奇。我對我的溫度和溼度傳感器使用相同的邏輯,並且效果很好。我想知道是什麼讓這段代碼卡在這條消息上。 – Thebrizkys

+0

您是否看到我關於緩存的評論?也許你的價值是None當你試圖寫它 – Francesco

+0

是的,我看到...如果它是None,我無法得到任何有關print「luminous emmittance」+ str(readLight())+「lx」的數據,但它會得到「發光輻射9.16666666667 lx」 – Thebrizkys