Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyMySQL未按预期进行插入_Python_Mysql_Insertion - Fatal编程技术网

Python PyMySQL未按预期进行插入

Python PyMySQL未按预期进行插入,python,mysql,insertion,Python,Mysql,Insertion,我有一张桌子,看起来像这样: 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. 我试图实现的是,对于每个标题,以及标题的每个注释,将注释分成不同的句子,并将其放在不同的表格中,在lucene_rs中必须如下所示: id |

我有一张桌子,看起来像这样:

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.
我试图实现的是,对于每个标题,以及标题的每个注释,将注释分成不同的句子,并将其放在不同的表格中,在lucene_rs中必须如下所示:

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 
我已经为此编写了代码,当我在控制台/终端上打印它时,它可以正常工作。它打印评论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的所有结果读入一个列表,然后在该列表上循环,要么使用不同的光标进行插入


不要注释掉您希望我们帮助的代码。所以的代码高亮显示使它很难阅读。嗨,Barmar,我已经取消了cursor.executeINSERT语句的注释。谢谢你的提醒!这就像是一种魅力,从我的角度来说,这真是一件愚蠢的事情。非常感谢巴尔马。
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()