3

I am new to django and i have been scouting the site to find good ways to store images generated using python(there seems to be contradicting views on saving it to a local folder, file or database). My website's sole purrpose is to make qrcodes all the time. In my local machine i would save it in my program folder like:

import pyqrcode
qr = pyqrcode.create("{'name': 'myqr'}")
qr.png("horn.png", scale=6)
print "All done!"

I am running into a situation where i will be dealing with many users doing the same thing. I doubt saving it to a local folder would be a viable option. I am set on saving these images as blobs in mysql. Has anyone done this sort of thing? If so, what was the best way to implement.Example code would also be helpful.

Edwinner
  • 2,458
  • 1
  • 22
  • 33

1 Answers1

3

Storing images in the database is somewhat an edge-case. To relieve the database, I would not store them in the database, but in a folder on your server.

I understand your question so, that some users might create the same qr-codes. In this case, I would create a database table like that:

CREATE TABLE qrcodes (
   value TEXT PRIMARY KEY,
   fname TEXT
);

With this table you can find the qr-codes by the encoded value. If an entry exists for a given value, than you can just return the contents of the file whose name is stored.

Leaves one question: How to create the file names. There are many possibilities. One would be to create an UUID and make a filename from it. An other option would be to use a global counter and give every file a new number. The counter must be stored in a database table of course.

Of course you can still store the image in the database, if you want. Just don't use the fname field, but a blob-field that stores the content. That solution should work on most databases, but is likely slower than the file approach when you have really big amounts of data.

Juergen
  • 12,378
  • 7
  • 39
  • 55
  • So, images as files in a folder. And a database to keep track. Very informative. I will try this approach since it is the only answer for now. – Edwinner Jun 20 '15 at 20:10