简体   繁体   English

使用 python 的 pymysql 数据库存储错误

[英]pymysql database storing error using python

i am having a error in this program, i am trying to store my output in a database collected from temperature sensor using arduino program我在这个程序中有一个错误,我正在尝试将我的 output 存储在使用 arduino 程序从温度传感器收集的数据库中

python code python代码

import serial
import pymysql

device = /dev/ttyACM0'
arduino = serial.Serial(device,9600)

data  = arduino.readline()
print (data)

connection = pymysql.conect("localhost","pi","","temperature_db) or die("cant connect to database")

print (connection)

with connection:
cursor = connection .cursor 
cursor.execute("INSERT INTO tempLog (Temperature) VALUES (%s)" %(data))
connection.commit()
connection.execute()

this is how I have created my database.这就是我创建数据库的方式。

数据库

and this is what i have written in my arduino code这就是我在 arduino 代码中写的代码

and this is the error I received for python code这是我收到的 python 代码错误

错误代码1

can anyone please help me to solve this issue and explain why I had that error and how it was fixed?谁能帮我解决这个问题并解释为什么我有这个错误以及它是如何解决的?

Try this:尝试这个:

cursor.execute(f"INSERT INTO tempLog (Temperature) VALUES ({data.decode('utf-8').strip()}" )

Explanation解释

There are two issues that need to be corrected:有两个问题需要纠正:

  • The byte variable data (b'Temp: 26 C\r\n') needs to be converted to a string and whitespaces ('r\n') stripped off using for example data.decode("utf-8").strip()字节变量data (b'Temp: 26 C\r\n')需要转换为字符串,并使用例如data.decode("utf-8").strip()去除空格 ('r\n') data.decode("utf-8").strip()

  • The sql insert statement expect a quoted string value like this VALUES ("Temp: 26C") . sql 插入语句需要一个带引号的字符串值,如VALUES ("Temp: 26C")

  1. Example of the different steps using Python使用 Python 的不同步骤示例
#
# The current sql insert look like this. Notice the value type
#
>>> "INSERT INTO tempLog (Temperature) VALUES (%s)" %(data)
"INSERT INTO tempLog (Temperature) VALUES (b'Temp: 26C\\r\\n')"

#
# Convert data to a string and strip white spaces
#
>>> data = data.decode("utf-8").strip()
>>> "INSERT INTO tempLog (Temperature) VALUES (%s)" %(data)
'INSERT INTO tempLog (Temperature) VALUES (Temp: 26C)'

#
# Finaly add quotes to VALUES to pass data as a string
#
>>> "INSERT INTO tempLog (Temperature) VALUES (\"%s\")" %(data)
'INSERT INTO tempLog (Temperature) VALUES ("Temp: 26C")'

#
# Final suggestion using python f-string
#
cursor.execute(f"INSERT INTO tempLog (Temperature) VALUES ({data.decode('utf-8').strip()}" )
  1. Your code with the suggested modifications.您的代码带有建议的修改。
import serial
import pymysql

device = /dev/ttyACM0'
arduino = serial.Serial(device,9600)

data  = arduino.readline()

data = data.decode("utf-8").strip() # Convert to str and strip whitespaces
print (data)

connection = pymysql.connect("localhost","pi","","temperature_db") or die("cant connect to database")

print (connection)

with connection:
    cursor = connection.cursor()

    sql = ("INSERT INTO tempLog (Temperature) VALUES (\"%s\");" % data)
    print(f"sql: {sql}") 

    try:
        cursor.execute(sql)
        connection.commit()
    except Exception as e:
        print(e) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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