python - 如何将python变量的值存储在phpmyadmin的数据库中?
问题描述
我创建了一个名为“坐标”的 python 列表变量。坐标列表由图中随机点的 x 轴和 y 轴值对组成。一对中的第一个值表示 x 轴值,第二个值表示 y 轴值。坐标列表变量如下所示。
coordinates = [(95, 171), (54, 131), (125, 131), (213, 78)]
我已经实现了一个名为“direction_finder”的函数,通过使用坐标列表变量中的那些 x 轴值和 y 轴值来查找基本方向。
该函数的实现步骤如下
1.访问坐标列表变量中每对中的x轴值。
x1 = coordinates[0][0]
x2 = coordinates[1][0]
x3 = coordinates[2][0]
x4 = coordinates[3][0]
2.访问坐标列表变量中每对的y轴值。
y1 = coordinates[0][1]
y2 = coordinates[1][1]
y3 = coordinates[2][1]
y4 = coordinates[3][1]
3.取x轴和y轴值的度数。
degrees_x1y1 = math.atan2(x1,y1)/math.pi* 180
degrees_x2y2 = math.atan2(x2,y2)/math.pi* 180
degrees_x3y3 = math.atan2(x3,y3)/math.pi* 180
degrees_x4y4 = math.atan2(x4,y4)/math.pi* 180
4.添加if条件取最终度数。
if degrees_x1y1 < 0:
degrees_final_x1y1 = 360 + degrees_x1y1
else:
degrees_final_x1y1 = degrees_x1y1
if degrees_x2y2 < 0:
degrees_final_x2y2 = 360 + degrees_x2y2
else:
degrees_final_x2y2 = degrees_x2y2
if degrees_x3y3 < 0:
degrees_final_x3y3 = 360 + degrees_x3y3
else:
degrees_final_x3y3 = degrees_x3y3
if degrees_x4y4 < 0:
degrees_final_x4y4 = 360 + degrees_x4y4
else:
degrees_final_x4y4 = degrees_x4y4
5.为基本方向创建一个数组。
direction_brackets = ["NORTH", "NORTH-EAST","EAST","SOUTH-EAST","SOUTH","SOUTH-WEST","WEST","NORTH-WEST"]
6.round最终度数
round_x1y1 = round(degrees_final_x1y1/45)
round_x2y2 = round(degrees_final_x2y2/45)
round_x3y3 = round(degrees_final_x3y3/45)
round_x4y4 = round(degrees_final_x4y4/45)
7.获取最终度数的最终基本方向
final_direction_x1y1 = direction_brackets[round_x1y1]
final_direction_x2y2 = direction_brackets[round_x2y2]
final_direction_x3y3 = direction_brackets[round_x3y3]
final_direction_x4y4 = direction_brackets[round_x4y4]
8.返回最终的基本方向值
return final_direction_x1y1,final_direction_x2y2, final_direction_x3y3, final_direction_x4y4
9.创建一个名为directions_list 的列表变量来收集direction_finder 函数返回的最终基本方向值。
direction_list = []
direction_list= direction_lookup(coordinates)
10.从“direction_list”变量中获取最终的基本方向。
direction_x1y1 = direction_list[0]
direction_x2y2 = direction_list[1]
direction_x3y3 = direction_list[2]
direction_x4y4 = direction_list[3]
实现代码后,我必须创建数据库连接以将最终的基本方向存储在 PHPMyAdmin 数据库中。我创建了一个数据库和一个单独的表来存储最终的基本方向。这些步骤的代码如下。
connection = pymysql.connect(host="localhost", user="root", passwd="", database="directions")
cursor = connection.cursor()
directionsCollect= """Create Table directions_store(
id Int(11) Primary Key Auto_Increment,
cardinal_directions Varchar(255),
)"""
cursor.execute(directionsCollect)
之后,我实现了插入命令以向创建的表插入方向。
record_x1y1 = """Insert Into directions_store(id, cardinal_directions)
Values(%(cardinal_directions)s)""",
{
'cardinal_directions': direction_x1y1
}
cursor.execute(record_x1y1)
#commit the connection
connection.commit()
#close the connection
connection.close()
但问题是插入没有发生。错误说如下。
Traceback (most recent call last):
File "floor_plan_detector.py", line 320, in <module>
cursor.execute(record_x1y1)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/cursors.py", line 163, in execute
result = self._query(query)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/connections.py", line 504, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/sameera/anaconda3/lib/python3.7/site-packages/pymysql/connections.py", line 762, in _execute_command
packet = prelude + sql[:packet_size-1]
TypeError: can't concat tuple to bytes
错误表示类型错误。任何人都可以解释这里有什么问题吗?
解决方案
除了你是怎么做的,
在您的台词中:
record_x1y1 = """Insert Into directions_store(id, cardinal_directions)
Values(%(cardinal_directions)s)""",
{
'cardinal_directions': direction_x1y1
}
cursor.execute(record_x1y1)
您正在创建一个元组: tuple(str, dict) 类型的 record_x1y1 然后将其传递给函数。
pymysql 的 cursor.execute 然后尝试用它做一些事情,但它不是它知道的东西,因此是错误。
你应该做的是分别定义查询和参数:
record_x1y1 = """Insert Into directions_store(id, cardinal_directions)
Values(%(cardinal_directions)s)"""
params = {
'cardinal_directions': direction_x1y1
}
cursor.execute(record_x1y1, params)
或者您可以将元组解包到 args:
record_x1y1 = """Insert Into directions_store(id, cardinal_directions)
Values(%(cardinal_directions)s)""",
{
'cardinal_directions': direction_x1y1
}
cursor.execute(*record_x1y1)
推荐阅读
- shibboleth - 登录后 Moodle 未从服务提供商处获取属性
- c - CLOCK_REALTIME 和 CLOCK_MONOTONIC 一次调用
- spring-boot - 使用 Mockito 和 Junit 5 进行 Spring Boot 单元测试,如何为用户设置密码
- reactjs - 如何使用 react 和 typescript 在子组件中渲染父组件?
- reactjs - TS2322:键入“HTMLElement | null' 不可分配给类型 'HTMLButtonElement'。在按下术语复选框时启用按钮和其他错误
- javascript - 为什么 document.evaluate 在手动突出显示 Web 元素后返回正确的值,但在浏览器控制台首次执行时无法返回?
- github - 备份 Jbmp/drools 工作台的 m2 存储库
- android - 使用动画自定义 EditText
- css - 为什么最后一个子选择器在 SCSS 中不起作用?
- flutter - 父小部件不使用继承更新