【问题标题】:how to store the python variable's value inside the database in phpmyadmin?如何将python变量的值存储在phpmyadmin的数据库中?
【发布时间】:2020-11-09 15:10:41
【问题描述】:

我创建了一个名为“坐标”的 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_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 = "THE QUERY", params = {"the": "param"}, and cursor.execute(record_x1y1, params)
  • PHPMyAdmin 不是数据库,是管理MySQL 数据库的PHP 软件。您不应该将您的方向存储为 varchar 列。最好使用两个数字列,每个元素一个列。

标签: python mysql


【解决方案1】:

你是怎么做的,

在你的台词中:

record_x1y1 = """Insert Into directions_store(id, cardinal_directions)
Values(%(cardinal_directions)s)""",
{
   'cardinal_directions': direction_x1y1
}

cursor.execute(record_x1y1)

您正在创建一个元组:record_x1y1 类型为 tuple(str, dict) 然后将其传递给函数。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多