1

I've been wanting to build a pretty big project which pretty much mimics Twitter, I've just started it and have run into a little error with account creation. I was wondering what the best method of storing and recalling data is. I'm well aware I can use .txt files to do this, but then I've found it's trickier to make the program recognize the structure of the data.

For example, the file might look like this:
acnt - twigman
pass - yellow

And the user creating an account might use the same name, and I would need to stop that, but I wouldn't know how to make the program know where the username starts and ends.

If I could use a database within the Python program, that would be a heck of a lot easier, it would mean the program can run as just one file and it would be easier to code. The program could also recognize the beginning and end of a user name without needing to write more script for it.

Thank you for your help.

3 Answers3

3

Your expectations of Python are very low. That is great news because you are going to be very happy.

You can definitely use a database with Python. The simplest and quickest to set up as well as closest to what you described is a file database: SQLite. Here is a great tutorial to start.

If you want to stick to simpler than than, you can use the pickle module or the shelve module to ‘persist’ your data

Of course, once your database increases significantly in size, you can move on to MySQL or PostgreSQL or others.

I encourage you to check out database abstraction tools such as SQLAlchemy as they can make your life easier.

If you are building a web application, then why reinvent the wheel? Choose one from the many Python web frameworks. Some if not all of them abstract away the database so you can focus on your application and its features.

Ben Beirut
  • 733
  • 3
  • 12
0

Connecting to a database is really the way to go here. This gets deep pretty fast, but as far as handling the python sytax it's fairly straightforward. This answer handles that subject well:

How do I connect to a MySQL Database in Python?

If you are truly going to be interacting with real users, you should learn about how to properly secure their passwords via hashing or other methods. Storing passwords in plain text is a big no-no. Here's an interesting place to start for password hashing in Python:

Python's safest method to store and retrieve passwords from a database

Community
  • 1
  • 1
  • Thanks for this, the txt file was a demo, of course, I'll be modifying it for actual potential use once I'm happy with the program itself! – Spookichicken Sep 04 '16 at 23:30
0

Use SQLite or any other RDBMS like PostgreSQL. To use Python with RDBMS: https://wiki.python.org/moin/DatabaseInterfaces

batinex
  • 33
  • 8