ZopeMag's mascot the ZOPE fish

import psycopg

def add_filedata(self, tbl_name, obj_id, title, file, REQUEST=None):
    " insert binary file into postgres sql table "
  
    # get the connection id of the Z SQL method that inserts the metadata
    db_con = self.sql_insertMetadata.connection_id

    # get the database connection object that is used by the Z SQL method "sql_insertMetadata"
    dba = self.aq_acquire(db_con)

    if title == '': 
        REQUEST.RESPONSE.redirect('addDocumentMethod?msg=Please+type+in+a+filename+.')

    # open a database connection and pass the connection string of the dba object
    o = psycopg.connect(dba.connection_string)

    # create cursor
    c = o.cursor()
    
    # check if the file already exists

    if tbl_name=='tbl_download' and  len(self.sql_checkDownload(file_name=title))!=0:
        REQUEST.RESPONSE.redirect('error_file?msg=This+file+already+exists.')
    else:


    	if tbl_name=='tbl_productinfo':

       		c.execute("INSERT INTO tbl_productinfo (obj_id, imagename, imagedata) VALUES (%(obj_id)i, %(img_title)s, %(data)s)",
               		   {'obj_id':obj_id, 'img_title':title, 'data':psycopg.Binary(file.read())}
                	 )
    
    	elif tbl_name=='tbl_download':

        	c.execute("INSERT INTO tbl_download (obj_id, filename, filedata) VALUES (%(obj_id)i, %(file_name)s, %(file_data)s)",
                           {'obj_id':obj_id, 'file_name':title, 'file_data':psycopg.Binary(file.read())}
                 	 )
   
    	# commit on the database connection
    	o.commit()

    	if REQUEST is not None:
            REQUEST.RESPONSE.redirect('index.html')



Home   Subscribe   FAQ   Contact   Write for us   Privacy Policy   Weekly News   PyZine   opensourcexperts.com  

Reproduction of material from any of ZopeMag's pages without prior written permission is strictly prohibited. Copyright 2003 - 2005 ZopeMag Zope/Plone hosting by Nidelven IT