Somthing wrong with using CSV as database for a webapp? Somthing wrong with using CSV as database for a webapp? flask flask

Somthing wrong with using CSV as database for a webapp?


Just don't do it.

The problem with CSV is …

a, concurrency is not possible: What this means is that when two people access your app at the same time, there is no way to make sure that they don't interfere with each other, making changes to each other's data. There is no way to solve this with when using a CSV file as a backend.

b, speed: Whenever you make changes to a CSV file, you need to reload more or less the whole file. Parsing the file is eating up both memory and time.

Databases were made to solve this issues.

I agree however, that you don't need to learn SQLAlchemy for a small app.

There are lightweight alternatives that you should consider.

What you are looking for are ORM - Object-relational mapping - who translate Python code into SQL and manage the SQL databases for you.

PeeweeORM and PonyORM. Both are easy to use and translate all SQL into Python and vice versa. Both are free for personal use, but Pony costs money if you use it for commercial purposes. I highly recommend PeeweeORM. You can start using SQLite as a backend with Peewee, or if your app grows larger, you can plug in MySQL or PostGreSQL easily.


Don't do it, CSV that is.

There are many other possibilities, for instance the sqlite database, python shelve, etc. The available options from the standard library are summarised here.

Given that your application is a webapp, you will need to consider the effect of concurrency on your solution to ensure data integrity. You could also consider a more powerful database such as postgres for which there are a number of python libraries.


I think there's nothing wrong with that as long as you abstract away from it. I.e. make sure you have a clean separation between what you write and how you implement i . That will bloat your code a bit, but it will make sure you can swap your CSV storage in a matter of days.

I.e. pretend that you can persist your data as if you're keeping it in memory. Don't write "openCSVFile" in you flask app. Use initPersistence(). Don't write "csvFile.appendRecord()". Use "persister.saveNewReport()". When and if you actually realise CSV to be a bottleneck, you can just write a new persister plugin.

There are added benefits like you don't have to use a mock library in tests to make them faster. You just provide another persister.