英文:
Why is data is not added to the cell?
问题
我可以告诉你为什么数据没有添加到指定的单元格吗?
我正在尝试收集列表中的所有数字并将它们放入一个单元格中。脚本执行成功,清空了一个单元格,但单元格中的数据没有出现。
需要将列表放入一个单元格中,因为这就是freebx的工作原理。
#/usr/bin/python3
import mysql.connector
try:
connection = mysql.connector.connect(host='localhost',
database='asterisk',
user='freepbxuser',
password='',
autocommit=True)
sql_select_Query = "SELECT CONCAT(cell) FROM userman_users"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
dirty_text = [i[0] for i in cursor.fetchall()]
for item in dirty_text.copy():
if item is None:
dirty_text.remove(item)
SYMBOLS = '{}()[].,:;+-*/&|<>=~'
clear_text = []
for element in dirty_text:
temp = ""
for ch in element:
if ch not in SYMBOLS:
temp += ch
clear_text.append(temp)
values = [list([item]) for item in clear_text]
cursor.executemany ("""
UPDATE pinsets
SET passwords=%s
WHERE pinsets_id=1
""", (values))
except mysql.connector.Error as e:
print("Error", e)
finally:
if connection.is_connected():
connection.close()
cursor.close()
请注意,此脚本将从数据库中检索的数据更新到名为pinsets
的表中的passwords
列中,其中pinsets_id
为1。确保数据库连接配置正确并且您有适当的权限来执行这些操作。如果仍然有问题,您可能需要检查数据库中的表结构和数据类型是否正确匹配。
英文:
Can you tell me why the data is not being added to the specified cell?
I'm trying to collect all the numbers in a list and put them in a cell. The script fulfills, clears a cell, but the data in a cell does not appear.
It is necessary to put the list in exactly one cell, because this is how freebx works
#/usr/bin/python3
import mysql.connector
try:
connection = mysql.connector.connect(host='localhost',
database='asterisk',
user='freepbxuser',
password='',
autocommit=True)
sql_select_Query = "SELECT CONCAT(cell) FROM userman_users"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
dirty_text = [i[0] for i in cursor.fetchall()]
for item in dirty_text.copy():
if item is None:
dirty_text.remove(item)
SYMBOLS = '{}()[].,:;+-*/&|<>=~'
clear_text = []
for element in dirty_text:
temp = ""
for ch in element:
if ch not in SYMBOLS:
temp += ch
clear_text.append(temp)
values = [list([item]) for item in clear_text]
cursor.executemany ("""
UPDATE pinsets
SET passwords=%s
WHERE pinsets_id=1
""", (values))
except mysql.connector.Error as e:
print("Error", e)
finally:
if connection.is_connected():
connection.close()
cursor.close()
答案1
得分: 1
这段代码实现了我认为你想要的功能。它从"userman_users"表的"cell"列中提取所有非NULL值,去除其中的特殊字符,然后将它们存储为以逗号分隔的列表,存储在"pinsets"表的一行中:
connection = mysql.connector.connect(host='localhost',
database='asterisk',
user='freepbxuser',
password='',
autocommit=True)
sql_select_Query = "SELECT cell FROM userman_users;"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
dirty_text = [i[0] for i in cursor.fetchall() if i[0]]
SYMBOLS = '{}()[].,:;+-*/&|<>=~'
clear_text = []
for element in dirty_text:
clear_text.append(''.join(ch for ch in element if ch not in SYMBOLS))
cursor.execute ("""
UPDATE pinsets
SET passwords=%s
WHERE pinsets_id=1;
""", (','.join(clear_text),)
)
请注意,这段代码是用Python编写的,用于从数据库中提取数据并进行处理。
英文:
This does what I THINK you were trying to do. This fetches all of the non-NULL values from the "cell" column from "userman_users", removes any special characters from them, and stores them as a comma-separated list in the one row in "pinsets":
connection = mysql.connector.connect(host='localhost',
database='asterisk',
user='freepbxuser',
password='',
autocommit=True)
sql_select_Query = "SELECT cell FROM userman_users;"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
dirty_text = [i[0] for i in cursor.fetchall() if i[0]]
SYMBOLS = '{}()[].,:;+-*/&|<>=~'
clear_text = []
for element in dirty_text:
clear_text.append( ''.join(ch for ch in element if ch not in SYMBOLS) )
cursor.execute ("""
UPDATE pinsets
SET passwords=%s
WHERE pinsets_id=1;
""", (','.join(clear_text),)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论