Ok, so i have this working, just wondering if there is an easier way to do this. In my database, i have a table called files, with 5 columns:
[ul]ID , which is of type INTEGER and is auto-incrementing
Tag, which is of type VARCHAR(200)
name, which is of type VARCHAR(200)
ext, which is of type VARCHAR(5)
data, which is a LONGBLOB.
[/ul]
On the windows, i have a List object, and 2 buttons, save and insert.
Save code
[code]file = event.source.parent.getComponent(‘List’).selectedIndex
if file != -1:
table = fpmi.db.toPyDataSet(event.source.parent.getComponent(‘List’).data)
id = table[file][‘id’]
ext = table[file][‘ext’]
name = table[file][‘name’] + “.” + ext
filename = fpmi.file.saveFile(name,ext,ext)
if filename != None:
resultset = fpmi.db.runQuery("select data from files where id = %d"%(id))
if len(resultset) > 0:
data = resultset[0][0]
writeToFile = 1
if fpmi.file.fileExists(filename):
writeToFile = fpmi.gui.confirm("File '%s' already exists. Overwrite?"%filename)
if writeToFile:
fpmi.file.writeFile(filename, data)
[/code]
Insert Code
#import re library
import re
#get tag of window
tag = event.source.parent.Tag
#prompt user to select file
path = fpmi.file.openFile()
#if user selects a file, and does not click cancel, do this
if path != None:
#find the extension
split1 = re.split(re.compile("\."),path)
for row in split1:
ext = row
#find the file name
split2 = re.split(re.compile("\\\\"),path)
for row in split2:
name = row
#remove the extension from the file name
name = re.sub("."+ext,"",name)
#read in the file as bytes
bytes = fpmi.file.readFileAsBytes(path)
#write the data to the database, and return the id of the new row
key = fpmi.db.runPrepStmtGetKey("INSERT INTO Files (tag,name,ext,data) VALUES (?,?,?,?)", (tag,name,ext,bytes))
#refresh the data in the list, as it is set to polling off
fpmi.db.refresh(event.source.parent.getComponent('List'),"data")
#set the selected value equal to the new key, doesnt work, the previous
#line of code should be a fpmi.db.runQuery with the data query
event.source.parent.getComponent('List').selectedValue = key
Does anyone know if there isw an easier, or neater way to do this, or there is any other file type actions we can add to this screen?
ps Is there a way to add attachements to posts so others can just paste this into their projects, and save them the hassle?
File Management.fwin (10.3 KB)