PyMySQL 没有按预期进行插入
PyMySQL not doing insertion as expected
我有一个 table lucene_try 看起来像这样:
id || title || comment
1 || Title 1 || Sentence 1 of Comment 1. Sentence 2 of Comment 1.
2 || Title 1 || Sentence 1 of Comment 2. Sentence 2 of Comment 2.
我想要实现的是针对每个标题和标题的每个评论,将评论分成不同的句子并将其放在不同的 table 中,在 [=23= 中必须看起来像这样]:
id || title || root_comment || sub_comment
1 || Title 1 || Comment 1 || Sentence 1
2 || Title 1 || Comment 1 || Sentence 2
3 || Title 1 || Comment 2 || Sentence 1
4 || Title 1 || Comment 2 || Sentence 2
我已经为此编写了代码,当我在 console/terminal 上打印时它可以正常工作。它打印评论 1 的句子 1 和句子 2 以及评论 2。但是,当我想插入此数据时,它只打印和插入评论 1 的句子 1 和句子 2。
这是我的代码:
import pymysql
import pymysql.cursors
import random
from bs4 import BeautifulSoup
import nltk
from nltk import tokenize
import pdb
conn = pymysql.connect(host='localhost', user='root', password='password', db='master_thesis', autocommit=True)
cursor = conn.cursor()
cursor.execute("SELECT * FROM lucene_counter WHERE count > 5 AND count <= 30")
lucene_rs_list = list()
for row in cursor:
lucene_rs_list.append(row[1])
random.shuffle(lucene_rs_list)
final_list = lucene_rs_list[:1]
for i in range(len(final_list)):
current_title = final_list[i]
query = "SELECT title, comment FROM lucene_try WHERE title = %s"
cursor.execute(query, final_list[i])
for row in cursor:
root_comment = BeautifulSoup(row[1], "lxml").text
print("Root Title: ", current_title)
print("Root Comment: ", root_comment)
cleancomment = tokenize.sent_tokenize(root_comment)
for j in range(len(cleancomment)):
# THIS LINE PRINTS EVERYTHING PROPERLY WITH ALL THE COMMENTS AND SUBCOMMENTS IF CURSOR.EXECUTE IS COMMENTED OUT
print("Sub Comment: ", cleancomment[j])
# IF THE CURSOR.EXECUTE IS UNCOMMENTED, IT ONLY DISPLAYS RESULT OF THE FIRST ROOT_COMMENT AND NOT ALL
cursor.execute("""INSERT INTO lucene_rs (title, root_comment, comment) VALUES ("%s", "%s", "%s")""" % (current_title, root_comment, cleancomment[j]))
print("\n")
conn.close()
问题是您在执行 INSERT
查询时使用的游标与获取 SELECT
查询的结果时使用的游标相同。当您执行 INSERT
时,它不再包含 SELECT
的结果,因此循环的下一次迭代停止。
要么将 SELECT
的所有结果读入一个列表,然后对其进行循环,要么对 INSERT
.
使用不同的游标
conn = pymysql.connect(host='localhost', user='root', password='password', db='master_thesis', autocommit=True)
cursor = conn.cursor()
cursor2 = conn.cursor()
cursor.execute("SELECT * FROM lucene_counter WHERE count > 5 AND count <= 30")
lucene_rs_list = list()
for row in cursor:
lucene_rs_list.append(row[1])
random.shuffle(lucene_rs_list)
final_list = lucene_rs_list[:1]
for i in range(len(final_list)):
current_title = final_list[i]
query = "SELECT title, comment FROM lucene_try WHERE title = %s"
cursor.execute(query, final_list[i])
for row in cursor:
root_comment = BeautifulSoup(row[1], "lxml").text
print("Root Title: ", current_title)
print("Root Comment: ", root_comment)
cleancomment = tokenize.sent_tokenize(root_comment)
for j in range(len(cleancomment)):
# THIS LINE PRINTS EVERYTHING PROPERLY WITH ALL THE COMMENTS AND SUBCOMMENTS IF CURSOR.EXECUTE IS COMMENTED OUT
print("Sub Comment: ", cleancomment[j])
# IF THE CURSOR.EXECUTE IS UNCOMMENTED, IT ONLY DISPLAYS RESULT OF THE FIRST ROOT_COMMENT AND NOT ALL
cursor2.execute("""INSERT INTO lucene_rs (title, root_comment, comment) VALUES ("%s", "%s", "%s")""" % (current_title, root_comment, cleancomment[j]))
print("\n")
conn.close()
我有一个 table lucene_try 看起来像这样:
id || title || comment
1 || Title 1 || Sentence 1 of Comment 1. Sentence 2 of Comment 1.
2 || Title 1 || Sentence 1 of Comment 2. Sentence 2 of Comment 2.
我想要实现的是针对每个标题和标题的每个评论,将评论分成不同的句子并将其放在不同的 table 中,在 [=23= 中必须看起来像这样]:
id || title || root_comment || sub_comment
1 || Title 1 || Comment 1 || Sentence 1
2 || Title 1 || Comment 1 || Sentence 2
3 || Title 1 || Comment 2 || Sentence 1
4 || Title 1 || Comment 2 || Sentence 2
我已经为此编写了代码,当我在 console/terminal 上打印时它可以正常工作。它打印评论 1 的句子 1 和句子 2 以及评论 2。但是,当我想插入此数据时,它只打印和插入评论 1 的句子 1 和句子 2。
这是我的代码:
import pymysql
import pymysql.cursors
import random
from bs4 import BeautifulSoup
import nltk
from nltk import tokenize
import pdb
conn = pymysql.connect(host='localhost', user='root', password='password', db='master_thesis', autocommit=True)
cursor = conn.cursor()
cursor.execute("SELECT * FROM lucene_counter WHERE count > 5 AND count <= 30")
lucene_rs_list = list()
for row in cursor:
lucene_rs_list.append(row[1])
random.shuffle(lucene_rs_list)
final_list = lucene_rs_list[:1]
for i in range(len(final_list)):
current_title = final_list[i]
query = "SELECT title, comment FROM lucene_try WHERE title = %s"
cursor.execute(query, final_list[i])
for row in cursor:
root_comment = BeautifulSoup(row[1], "lxml").text
print("Root Title: ", current_title)
print("Root Comment: ", root_comment)
cleancomment = tokenize.sent_tokenize(root_comment)
for j in range(len(cleancomment)):
# THIS LINE PRINTS EVERYTHING PROPERLY WITH ALL THE COMMENTS AND SUBCOMMENTS IF CURSOR.EXECUTE IS COMMENTED OUT
print("Sub Comment: ", cleancomment[j])
# IF THE CURSOR.EXECUTE IS UNCOMMENTED, IT ONLY DISPLAYS RESULT OF THE FIRST ROOT_COMMENT AND NOT ALL
cursor.execute("""INSERT INTO lucene_rs (title, root_comment, comment) VALUES ("%s", "%s", "%s")""" % (current_title, root_comment, cleancomment[j]))
print("\n")
conn.close()
问题是您在执行 INSERT
查询时使用的游标与获取 SELECT
查询的结果时使用的游标相同。当您执行 INSERT
时,它不再包含 SELECT
的结果,因此循环的下一次迭代停止。
要么将 SELECT
的所有结果读入一个列表,然后对其进行循环,要么对 INSERT
.
conn = pymysql.connect(host='localhost', user='root', password='password', db='master_thesis', autocommit=True)
cursor = conn.cursor()
cursor2 = conn.cursor()
cursor.execute("SELECT * FROM lucene_counter WHERE count > 5 AND count <= 30")
lucene_rs_list = list()
for row in cursor:
lucene_rs_list.append(row[1])
random.shuffle(lucene_rs_list)
final_list = lucene_rs_list[:1]
for i in range(len(final_list)):
current_title = final_list[i]
query = "SELECT title, comment FROM lucene_try WHERE title = %s"
cursor.execute(query, final_list[i])
for row in cursor:
root_comment = BeautifulSoup(row[1], "lxml").text
print("Root Title: ", current_title)
print("Root Comment: ", root_comment)
cleancomment = tokenize.sent_tokenize(root_comment)
for j in range(len(cleancomment)):
# THIS LINE PRINTS EVERYTHING PROPERLY WITH ALL THE COMMENTS AND SUBCOMMENTS IF CURSOR.EXECUTE IS COMMENTED OUT
print("Sub Comment: ", cleancomment[j])
# IF THE CURSOR.EXECUTE IS UNCOMMENTED, IT ONLY DISPLAYS RESULT OF THE FIRST ROOT_COMMENT AND NOT ALL
cursor2.execute("""INSERT INTO lucene_rs (title, root_comment, comment) VALUES ("%s", "%s", "%s")""" % (current_title, root_comment, cleancomment[j]))
print("\n")
conn.close()