简体   繁体   English

如何在phpmyadmin的数据库中存储python变量的值?

[英]how to store the python variable's value inside the database in phpmyadmin?

I created a python list variable called 'coordinates'.我创建了一个名为“坐标”的 python 列表变量。 The coordinates list consists of the x-axis and y-axis value pairs of random points in a graph.坐标列表由图形中随机点的 x 轴和 y 轴值对组成。 The first value in a pair represents the x-axis value and the second value represents the y-axis value.一对中的第一个值表示 x 轴值,第二个值表示 y 轴值。 The coordinates list variable seems like below.坐标列表变量如下所示。

coordinates = [(95, 171), (54, 131), (125, 131), (213, 78)]

I have implemented a function called 'direction_finder' to find cardinal directions by using those x-axis values and y-axis values in the coordinates list variable.我已经实现了一个名为“direction_finder”的函数,通过使用坐标列表变量中的 x 轴值和 y 轴值来查找基本方向。

The implementation steps of the function as follows该函数的实现步骤如下

1.access to x-axis values in each pair in the coordinates list variable. 1.访问坐标列表变量中每对中的x轴值。

 x1 = coordinates[0][0]
 x2 = coordinates[1][0]
 x3 = coordinates[2][0]
 x4 = coordinates[3][0]

2.access to y-axis values in each pair in the coordinates list variable. 2.访问坐标列表变量中每对中的y轴值。

 y1 = coordinates[0][1]
 y2 = coordinates[1][1]
 y3 = coordinates[2][1]
 y4 = coordinates[3][1]

3.Take the value of the degrees of x-axis and y-axis values. 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.Add an if condition to take the final degree value. 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.Create an array for Cardinal Directions. 5.为基本方向创建一个数组。

direction_brackets = ["NORTH", "NORTH-EAST","EAST","SOUTH-EAST","SOUTH","SOUTH-WEST","WEST","NORTH-WEST"]

6.round the final degree values 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.Get the final cardinal directions for final degree values 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 the final cardinal direction values 8.返回最终的基本方向值

 return final_direction_x1y1,final_direction_x2y2, final_direction_x3y3, final_direction_x4y4

9.create a list variable called directions_list to gather the final cardinal directions values returned by the direction_finder function. 9.创建一个名为directions_list 的列表变量来收集direction_finder 函数返回的最终基本方向值。

direction_list = []
direction_list= direction_lookup(coordinates)

10.Get the final cardinal directions from the 'direction_list' variable. 10.从'direction_list'变量中获取最终的主要方向。

direction_x1y1 = direction_list[0]
direction_x2y2 = direction_list[1]
direction_x3y3 = direction_list[2]
direction_x4y4 = direction_list[3]

After implemented the code, I had to create the database connection to store the final cardinal directions in the PHPMyAdmin database.I created a database and a separate table to store the final cardinal directions.实现代码后,我必须创建数据库连接以在 PHPMyAdmin 数据库中存储最终的基本方向。我创建了一个数据库和一个单独的表来存储最终的基本方向。 The code for those stpes are as follows.这些 stpes 的代码如下。

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)

After that I implemented the insert command to insert a direction to the created table.之后,我实现了插入命令以向创建的表插入一个方向。

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() 

But the problem is that the insertion was not happend.但问题是插入没有发生。 The error said as below.错误如下。

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

The error said the type error.错误表示类型错误。 Can anybody explain what's the wrong in here?.任何人都可以解释这里有什么问题吗?

How you are doing it aside,抛开你是怎么做的,

In your lines:在您的行中:

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

cursor.execute(record_x1y1)

You are creating a tuple: record_x1y1 of type tuple(str, dict) Then passing it to the function as such.您正在创建一个元组: tuple(str, dict) 类型的 record_x1y1 然后将它传递给函数。

The pymysql's cursor.execute then tries to do something with it but it is not something it knows, thus the error. pymysql 的 cursor.execute 然后尝试用它做一些事情,但它不是它知道的东西,因此是错误。

What you should do is define the query and params seperately:您应该做的是分别定义查询和参数:

record_x1y1 = """Insert Into directions_store(id, cardinal_directions)
Values(%(cardinal_directions)s)"""

params = {
   'cardinal_directions': direction_x1y1
}

cursor.execute(record_x1y1, params)

Or you could unpack the tuple to args:或者您可以将元组解包为 args:

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

cursor.execute(*record_x1y1)

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

相关问题 在Python的类中更改变量的值 - Changing a variable's value inside a class in python 如何将密码查询值存储到python函数内的变量中? - How can I store the cypher query value into a variable inside python function? 如何在不丢失 python 中的变量值的情况下从数据库中存储和获取带有连接变量的字符串 - How to store and get strings with concatenated variables from database without loosing variable value in python 如何将元组值存储为python变量 - how to store tuple value as python variable 如何将awk中的值存储在Python变量中 - How to store a value from awk in a Python variable 如何在python的store proc中解析变量值 - How to parse variable value in store proc in python Python PyQt5:将图像数据存储到phpmyadmin数据库中 - Python PyQt5: Store image data into a phpmyadmin database django 中如何为变量添加默认值并存储在数据库中? - How do I add a default value to a variable and store in the database in django? 无法在“with”块内更改函数变量的值 python - Can't change a value of a function's variable inside a "with" block python 如何将 mysql 数据库中的列总和存储到 python 变量? - How to store the sum of a column from a mysql database to a python variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM