python - 从 Python 将值插入 MySQL 时出现“未定义”错误
问题描述
我用 python 运行代码,数据来自它。我想保存在 MySQL RDS 但我有错误:
('Connected to MySQL Server version ', '5.7.26-log')
("You're connected to database: ", (u'attendance1',))
MySQL connection is closed
Traceback (most recent call last):
File "v8.py", line 79, in <module>
mySql_insert_query = ("""INSERT INTO empolyee (ID, Name, date, Similarity) VALUES (%s",%s",%s",%s")""",(n+1, name, date_, smlrty_Prcnt))
NameError: name 'name' is not defined
编码:
import boto3
import io
from PIL import Image
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
n=0
rekognition = boto3.client('rekognition', region_name='eu-west-1')
dynamodb = boto3.client('dynamodb', region_name='eu-west-1')
image = Image.open("sample.jpeg")
stream = io.BytesIO()
image.save(stream,format="JPEG")
image_binary = stream.getvalue()
response = rekognition.search_faces_by_image(
CollectionId='test_Colction',
Image={'Bytes':image_binary}
)
for match in response['FaceMatches']:
smlrty_Prcnt = match['Similarity']
def prnt():
#global name
face = dynamodb.get_item(
TableName='test_Colction_Table',
Key={'RekognitionId': {'S': match['Face']['FaceId']}}
)
if 'Item' in face:
name = (face['Item']['FullName']['S'])
if name == 'basem tarek' :
print'Employee: ', name
date_ = response['ResponseMetadata']['HTTPHeaders']['date']
print 'Date: ', date_
try:
connection = mysql.connector.connect(*********,
user="******",
passwd="******",
database="attendance1")
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
try:
mySql_insert_query = ("""INSERT INTO empolyee (ID, Name, date, Similarity) VALUES (%s,%s,%s,%s)""",(n+1, name, date_, smlrty_Prcnt))
cursor = connection.cursor()
result = cursor.execute(mySql_insert_query)
connection.commit()
print("Record inserted successfully into table")
cursor.close()
except mysql.connector.Error as error:
print("Failed to insert record into table {}".format(error))
finally:
if (connection.is_connected()):
connection.close()
print("MySQL connection is closed")
解决方案
推荐阅读
- activemq - 在 Apache ActiveMQ 库中,在 5.7.0 和 5.8.0 版本之间的 `xbean` 发生了什么?
- python - 修复 Python 3.7 中的 base64 编码错误
- javascript - 使用 Babel *from vanilla js* 将如何使用已翻译成 vanilla JS 的 ES6 模块
- reactjs - react 正在编辑所有数组对象,而不仅仅是一个数组对象
- php - 如何将 AJAX 响应作为更新发布给所有登录的用户
- java - 从 InputStreamReader 读取字节进入无限循环
- c - 计算两个日期之间的天数
- c++ - 在 C++ 中将子类数组作为参数传递给父类的子类时出现问题
- javascript - 如何返回所有列表项并在屏幕上显示所有列表项/
- python-3.x - 将函数应用于Python中的每一行时出错