Python 脚本 - mysql.connector 不更新 MariaDB table
Python Script - mysql.connector doesn't update MariaDB table
一段时间以来,我一直在努力弄清楚为什么我的 python 脚本没有更新我的数据库。
我有一个名为 database.py
的文件,它是这样的:
import mysql.connector as mariadb
import globals
def connect():
mariadb_connection = mariadb.connect(user=globals.MY_USERNAME, password=globals.MY_PASSWORD, database='ellen')
return mariadb_connection
def insert_author_topics(author_id, topics, cursor):
sql = "UPDATE author SET topics = \'" + str(topics) + "\' WHERE id = " + str(author_id) + ";"
try:
cursor.execute(sql)
except mariadb.Error as error:
print("Error: {}".format(error))
def close(connection):
connection.commit()
connection.close()
在我的主文件中,我有这样一个函数:
def get_topics(x, ldamodel, cursor, connection):
author_text = x['processed_text']
bow_vector = dictionary.doc2bow([author_text])
topics = '['
for index, score in sorted(ldamodel[bow_vector], key=lambda tup: -1*tup[1]):
new_topic = "Score: {}\t Topic: {} ".format(score, ldamodel.print_topic(index, 5))
topics += new_topic
topics += ']'
database.insert_author_topics(author['id'], topics, cursor)
connection.commit()
我有一个作者的 pandas 数据框,并且 get_topics
调用 insert_author_topics
像这样调用每个作者:
authors.apply(lambda x: get_topics(x, ldamodel, cursor, connection), axis=1)
我总共有大约 100,000 位作者。出于某种原因,每当我 运行 这个脚本时,数据库中只有大约 200 位作者被更新。
我尝试 运行 脚本时没有收到任何错误。我尝试打印出每个使用 sql 语句更新的作者的 ID,我打印出了大约 100,000 个 ID。
如有任何帮助,我们将不胜感激。如果需要,我可以提供更多信息。谢谢!
问题出在这行代码中:
database.insert_author_topics(author['id'], topics, cursor)
如果你看看我传递给函数的参数,我实际上应该这样调用 insert_author_topics
:
database.insert_author_topics(x['id'], topics, cursor)
我不完全确定这是如何在不抛出任何错误的情况下工作的,但很高兴我终于弄明白了!
一段时间以来,我一直在努力弄清楚为什么我的 python 脚本没有更新我的数据库。
我有一个名为 database.py
的文件,它是这样的:
import mysql.connector as mariadb
import globals
def connect():
mariadb_connection = mariadb.connect(user=globals.MY_USERNAME, password=globals.MY_PASSWORD, database='ellen')
return mariadb_connection
def insert_author_topics(author_id, topics, cursor):
sql = "UPDATE author SET topics = \'" + str(topics) + "\' WHERE id = " + str(author_id) + ";"
try:
cursor.execute(sql)
except mariadb.Error as error:
print("Error: {}".format(error))
def close(connection):
connection.commit()
connection.close()
在我的主文件中,我有这样一个函数:
def get_topics(x, ldamodel, cursor, connection):
author_text = x['processed_text']
bow_vector = dictionary.doc2bow([author_text])
topics = '['
for index, score in sorted(ldamodel[bow_vector], key=lambda tup: -1*tup[1]):
new_topic = "Score: {}\t Topic: {} ".format(score, ldamodel.print_topic(index, 5))
topics += new_topic
topics += ']'
database.insert_author_topics(author['id'], topics, cursor)
connection.commit()
我有一个作者的 pandas 数据框,并且 get_topics
调用 insert_author_topics
像这样调用每个作者:
authors.apply(lambda x: get_topics(x, ldamodel, cursor, connection), axis=1)
我总共有大约 100,000 位作者。出于某种原因,每当我 运行 这个脚本时,数据库中只有大约 200 位作者被更新。
我尝试 运行 脚本时没有收到任何错误。我尝试打印出每个使用 sql 语句更新的作者的 ID,我打印出了大约 100,000 个 ID。
如有任何帮助,我们将不胜感激。如果需要,我可以提供更多信息。谢谢!
问题出在这行代码中:
database.insert_author_topics(author['id'], topics, cursor)
如果你看看我传递给函数的参数,我实际上应该这样调用 insert_author_topics
:
database.insert_author_topics(x['id'], topics, cursor)
我不完全确定这是如何在不抛出任何错误的情况下工作的,但很高兴我终于弄明白了!