• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

Stabilize Code DatabaseError: database disk image is malformed

Can any one tell me how to stabilize my code ... every once in a while when writting to the permdb i get this error ...

Exception in thread Thread-3:
Traceback (most recent call last):
  File "C:\Python26\lib\threading.py", line 532, in __bootstrap_inner
    self.run()
  File "c:\Python26\sign\geterror.py", line 6711, in run
    c.execute('select * from ptrecords')
DatabaseError: database disk image is malformed

Open in new window


I have started to erase the db files after it is sent that slows the error down but doesnt completely eliminate it... Is there some way to stabilize the code ?


Sending code
class SynceThread(threading.Thread):
    def __init__(self, parent, event):
        self.parent = parent #reference to main window
        self.event = event
        threading.Thread.__init__(self)
    def run(self):	   
	f =open("ipaddress.txt","r")
	self.ip = f.readline() # reads the lines
	f.close()
	host=self.ip	
        port = 51269
        addr = (host,port)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            f = open('TransferData.sqlite', "r")
            data = f.read()
            f.close()
            s.connect(addr)
            s.sendall(data)
            s.close()
	    self.cleardatabase()
	    self.parent.syncsuccess(self.event)
	    
            return True
        except socket.error:
	    traceback.print_exc(file=open("errlog.txt","a"))
	    exc_type, exc_value, exc_tb = sys.exc_info()
	    self.parent.syncerror2(self.event)
            
        except IOError:
	    traceback.print_exc(file=open("errlog.txt","a"))
	    exc_type, exc_value, exc_tb = sys.exc_info()
	    self.parent.syncerror2(self.event)
        return False 
    def cleardatabase(self):
	connect = sqlite3.connect('TransferData.sqlite')
	connect.execute("delete from ptrecords")
	connect.commit()

Open in new window

   
recieving Code

class ThreadClass(threading.Thread,InsertData):
    def setParent(self,p):
	self.parent = p
    def run(self):

        host = ''
	port = 51269
	backlog = 5
	size = 1024
	addr = (socket.gethostname(), port)
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.bind((addr))

	while 1:
	    s.listen(backlog)
	    client, addr= s.accept()    
            data = ""

            while 1:
                d = client.recv(size)
                if not d: 
                    break
                data += d
    
            client.close()
    
            fname="Temp.sqlite"
            f = open(fname, 'wb')    # notice the binary mode again
            f.write(data)
            f.close()
	   #get transferred information
	    conn = lite.connect("Temp.sqlite")
	    c = conn.cursor()
	    c.execute('select * from ptrecords')
	    for rows in c:
		Actual_Incident_number=rows[0]
		Incident_number = rows[1]
		Last_Name = rows[2]
		First_Name = rows[3]
		Age= (str(rows[4]))
		Gender = rows[5]
		Address = rows[6]
		City= rows[7]
		State = rows[8]
		Zip = (str(rows[8]))
		Ailment = rows[10]
		Treatment = rows[11]
		Patient_reprt= rows[12]
		Initial_contact= (str(rows[13]))
		Hospital = (str(rows[14]))
		Inservice= (str(rows[16]))
		Destination= (rows[15])
		Provider_1 =(rows[17])
		Provider_2 = (rows[18])
		Sys =(str(rows[19]))
		Dia=(str(rows[20]))
		Pulse=(str(rows[21]))
		Resp=(str(rows[22]))
		Weather=(str(rows[23]))
		Temp=(str(rows[24]))
		Humid=(str(rows[25]))
		Wind=(str(rows[26]))
		TimeStamp=(str(rows[27]))
		Refusal=rows[28]
		image =(rows[29])

	   
		#LOAD  information into new database
		con = lite.connect('PermPatRecord.sqlite')
		cur = con.cursor()
		cur.execute('insert or replace into ptrecords values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
		    (Actual_Incident_number,Incident_number, Last_Name, First_Name, Age,Gender,Address,City,State,Zip
		     ,Ailment,Treatment,Patient_reprt,Initial_contact,Hospital,Destination,Inservice,
		     Provider_1,Provider_2,Sys,Dia,Pulse,Resp,Weather, Temp,Humid,Wind,TimeStamp,Refusal,image))
		con.commit()
		cur.close()
		con.close()
	    c.close()
	    conn.close()
	    self.repopulate()
	    os.remove('Temp.sqlite')

Open in new window

0
dolamitejenkins
Asked:
dolamitejenkins
  • 2
1 Solution
 
clockwatcherCommented:
I'm not sure that it matters since you're doing a full slurp with a read() and I'm not on a windows system to test it where it would actually apply, but based on the error I would try doing a binary read of the file you're sending:

  f = open('TransferData.sqlite', "rb")

And see if that helps.
0
 
dolamitejenkinsAuthor Commented:
Thanks I will try it
0
 
dolamitejenkinsAuthor Commented:
Thanks
0
 
peprCommented:
@clockwatcher: Congratulations for the new certificate ;)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now