0

I created a web crawler and my app was working without any error before but it was slow, so I decided to make it process two pages in same time using threading, everything working fine until comes to inserting data into database in last line in compare function using mysql connector, it keeps throwing this error

  File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor_cext.py", line 242, in execute
    raise errors.ProgrammingError("Cursor is not connected", 2055)
mysql.connector.errors.ProgrammingError: 2055: Cursor is not connected
from threading import Thread  
import datetime   
import mysql.connector  

class PostMaker:
    def __init__(self):
        pass
   
    def mysqlconnect(self,):
        db =  mysql.connect(host="localhost",
                        username="root",
                        password="",
                        database ="test"
        )
        cursor =  db.cursor()
        return db , cursor
    
    def createpost(self,page_id,post_name,post_slug,date,post_data):
        db ,cursor = self.mysqlconnect()
        query_1 = f"""INSERT INTO wp_posts (page_id,post_name,post_slug,date,post_data) Values ({page_id},
                "{post_name}","{post_slug}","{date}","{post_data}");"""

        cursor.execute(query_1)
        db.commit()
        post_id = cursor.lastrowid
        return post_id
    
    
       
    def compare(self,new_posts,**kwargs):
        for new_post in new_posts:
            post_slug = kwargs["post_slug"]
            post_name =  "post "+str(new_post)
            
            post_data = kwargs["post_data"]
            date  = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            #  here where the erorr happend
            post_id =  self.createpost(kwargs["page_id"],post_name,post_slug,date,post_data)
          
          
            
    def sub_list(self,id_list):
        sublist = list()
        for i in range(0, len(id_list),4):
            sublist.append(id_list[i:i + 4])
        return sublist  
           
    def page_handler(self,pages):
        for signle_page in pages:
            new_posts , info =  self.page_handler(signle_page)
            new_post =  self.compare(new_posts,**info)

            
    def  run(self):
        big_list=  self.sub_list(self.get_no_scan_data())
        for sub_list in big_list:
            new_thread = Thread(target=self.page_handler,args=(sub_list,))
            new_thread.start()  

Dan
  • 3,647
  • 5
  • 20
  • 26
urek mazino
  • 130
  • 12
  • This URL may be of value to you - https://stackoverflow.com/questions/5504340/python-mysqldb-connection-close-vs-cursor-close - from 2011, may still be valid advice. Not verified by me. – Wilson Hauck May 29 '22 at 12:48
  • @Wilson Hauck this not related to my problem , since I'm not trying to clo7the connection but the connection is lost its self – urek mazino May 29 '22 at 14:43
  • If you had a connection, how/why did it get lost? A General Log Analysis may be appropriate to follow the breadcrumbs. View pofile for contact information, if needed. – Wilson Hauck May 29 '22 at 21:23

0 Answers0