# Reading/Writing Files to the Database

**URL:** <https://forum.inductiveautomation.com/t/reading-writing-files-to-the-database/428>\
**Category:** Archived Topics\
**Created:** [December 5, 2007, 8:47pm UTC](https://forum.inductiveautomation.com/t/reading-writing-files-to-the-database/428 "2007-12-05T20:47:05Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [December 10, 2007, 10:20pm UTC](https://forum.inductiveautomation.com/t/reading-writing-files-to-the-database/428/21 "2007-12-10T22:20:57Z")

</div>

Yes, the line 16 in my code doesnt refernce event. All the try… except does is checks for the files window to be open, and if its open, it brings the window to the front, and updates the list. If the windows is not open, it just pops up a confirmation window to tell you the file has finished uploading. I want to try make a progress bar for the upload, but a) have no idea how, and b) dont have time right now to figure it out. Its something i will look at in the new year maybe, unless Carl knows of a way to do this.

Uploading the most recent window now  
[File Management.fwin](https://us1.discourse-cdn.com/inductiveautomation/original/1X/86d488d949508810f5c35d6090cb6e2504fe85fb.fwin) (10.3 KB)

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [December 10, 2007, 10:29pm UTC](https://forum.inductiveautomation.com/t/reading-writing-files-to-the-database/428/22 "2007-12-10T22:29:41Z")

</div>

> [@Kyle Chase](#):
>
> I want to try make a progress bar for the upload, but ...

You currently don't have any sort of ability to do something like this. We'd have to give you some sort of feedback as the client uploads the bytes to the Gateway, but you'd never get it all that accurate, as you're also at the mercy of the Gateway inserting the data over JDBC, which getting feedback for would be quite hard. Some sort of "please wait" animated screen would of couse be easy, but true progress bar, not so much.

I'll put a feature request for such feedback on the "Someday" list

---

<div class="post-metadata">

**Author:** ![Step7](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@Step7](https://forum.inductiveautomation.com/u/Step7)\
**Post date:** [December 10, 2007, 11:04pm UTC](https://forum.inductiveautomation.com/t/reading-writing-files-to-the-database/428/23 "2007-12-10T23:04:07Z")

</div>

[quote=“Carl.Gould”]  
Sorry, there were too many scripts posted here for me to re-create exactly what script you’re using. Please post the script.[/quote]

Ok, here is the code that works:

```auto
#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: 
	import fpmi
	ext = path[path.rfind(".")+1:]
	name = path[path.rfind("\")+1:len(path)-len(ext)-1]
	#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 
	fpmi.db.runPrepStmt("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") 
```

This is the code that doesn’t work:

```auto
#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: 
	def insertFile(path = path, tag = tag):
		import fpmi
		ext = path[path.rfind(".")+1:]
		name = path[path.rfind("\")+1:len(path)-len(ext)-1]
		#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 
		fpmi.db.runPrepStmt("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") 
	fpmi.system.invokeAsynchronous(insertFile)
```

I haven’t tried the new code that Kyle just posted yet. I just want to understand what is going on with the above code first. I’m not a Python expert (yet).

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [December 10, 2007, 11:13pm UTC](https://forum.inductiveautomation.com/t/reading-writing-files-to-the-database/428/24 "2007-12-10T23:13:56Z")

</div>

Whats going on is that the block that doesn’t work is trying to run the work through a call to “fpmi.system.invokeAsynchronous”. This means that it will be done in the background, allowing the button “unpress” and the GUI to keep responding.

Doing this involves wrapping the code in a new function (def insertFile). When you create a new function, the code in that function creates a new scope. This means that the use of the variable “event” in the second to last line is illegal, because there is no variable named “event” in insertFile’s namespace. You can pass event into the namespace in the def, like this:

`#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: 
   def insertFile(path = path, tag = tag, event=event): 
      import fpmi 
      ext = path[path.rfind(".")+1:] 
      name = path[path.rfind("\\")+1:len(path)-len(ext)-1] 
      #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 
      fpmi.db.runPrepStmt("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") 
   fpmi.system.invokeAsynchronous(insertFile)`

Hope this helps,

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [December 10, 2007, 11:35pm UTC](https://forum.inductiveautomation.com/t/reading-writing-files-to-the-database/428/25 "2007-12-10T23:35:39Z")

</div>

Beat me to it Carl. THe updated window i recently posted included the event = event in the call.

Step7, if you make any changes to the screen, please post it back to the forum for some insight or features i may have missed

[Previous page](https://forum.inductiveautomation.com/t/reading-writing-files-to-the-database/428.md?page=1)
