Let me first fix a misunderstanding here: NC does not store the file in the database but on the hard disk. In the database, there is just a cache of all files plus some meta data for simpler handling.
If you only want to access files existing in the NC instance (the ones that you can see in the web frontend), then there is a PHP API available, that allows you to access/read/write/modify/delete/list/… these files and folders. The overall API docs conatins this in general but might be a bit hard to find. In the official docs, there is also a section about the file system.
If you combine both, you should be able to tackle your problem, I guess.
If the problem is clearer to me, I might give better advice. Hope, this is sufficient for now.