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.
我一直想建立一个相当大的项目来模仿Twitter,我刚开始做,但在创建账号时遇到了一点错误。我想知道存储和回忆数据的最好方法是什么。我很清楚我可以使用。txt文件来实现这一点,但是我发现让程序识别数据的结构比较困难。
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.
如果我可以在Python程序中使用一个数据库,那将会非常容易,这意味着程序可以只运行一个文件,而且代码更容易编写。该程序还可以识别用户名的开头和结尾,而不需要为其编写更多的脚本。
Thank you for your help.
谢谢你的帮助。
3 个解决方案
#1
2
Your expectations of Python are very low. That is great news because you are going to be very happy.
您对Python的期望非常低。这是个好消息,因为你会很开心。
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.
您可以使用带有Python的数据库。最简单、最快的设置和最接近您所描述的是文件数据库:SQLite。这里有一个很好的入门教程。
If you want to stick to simpler than than, you can use the pickle module or the shelve module to ‘persist’ your data
如果您希望坚持使用比than更简单的方法,可以使用pickle模块或shelve模块来“持久化”您的数据
Of course, once your database increases significantly in size, you can move on to MySQL or PostgreSQL or others.
当然,一旦数据库的大小显著增加,您可以继续使用MySQL或PostgreSQL或其他数据库。
I encourage you to check out database abstraction tools such as SQLAlchemy as they can make your life easier.
我鼓励您检查数据库抽象工具,如SQLAlchemy,因为它们可以使您的生活更轻松。
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.
如果您正在构建一个web应用程序,那么为什么要重新发明*呢?从许多Python web框架中选择一个。有些(如果不是全部的话)会将数据库抽象出来,以便您可以专注于应用程序及其特性。
#2
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:
连接到数据库实际上就是到这里的方法。这一过程非常迅速,但就处理python sytax而言,它非常简单。这个答案很好地解决了这个问题:
How do I connect to a MySQL Database in Python?
如何在Python中连接到MySQL数据库?
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中进行密码哈希的一个有趣的地方:
Python's safest method to store and retrieve passwords from a database
Python最安全的从数据库中存储和检索密码的方法
#3
0
Use SQLite or any other RDBMS like PostgreSQL. To use Python with RDBMS: https://wiki.python.org/moin/DatabaseInterfaces
使用SQLite或任何其他RDBMS,如PostgreSQL。使用Python与RDBMS: https://wiki.python.org/moin/databaseinterface
#1
2
Your expectations of Python are very low. That is great news because you are going to be very happy.
您对Python的期望非常低。这是个好消息,因为你会很开心。
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.
您可以使用带有Python的数据库。最简单、最快的设置和最接近您所描述的是文件数据库:SQLite。这里有一个很好的入门教程。
If you want to stick to simpler than than, you can use the pickle module or the shelve module to ‘persist’ your data
如果您希望坚持使用比than更简单的方法,可以使用pickle模块或shelve模块来“持久化”您的数据
Of course, once your database increases significantly in size, you can move on to MySQL or PostgreSQL or others.
当然,一旦数据库的大小显著增加,您可以继续使用MySQL或PostgreSQL或其他数据库。
I encourage you to check out database abstraction tools such as SQLAlchemy as they can make your life easier.
我鼓励您检查数据库抽象工具,如SQLAlchemy,因为它们可以使您的生活更轻松。
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.
如果您正在构建一个web应用程序,那么为什么要重新发明*呢?从许多Python web框架中选择一个。有些(如果不是全部的话)会将数据库抽象出来,以便您可以专注于应用程序及其特性。
#2
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:
连接到数据库实际上就是到这里的方法。这一过程非常迅速,但就处理python sytax而言,它非常简单。这个答案很好地解决了这个问题:
How do I connect to a MySQL Database in Python?
如何在Python中连接到MySQL数据库?
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中进行密码哈希的一个有趣的地方:
Python's safest method to store and retrieve passwords from a database
Python最安全的从数据库中存储和检索密码的方法
#3
0
Use SQLite or any other RDBMS like PostgreSQL. To use Python with RDBMS: https://wiki.python.org/moin/DatabaseInterfaces
使用SQLite或任何其他RDBMS,如PostgreSQL。使用Python与RDBMS: https://wiki.python.org/moin/databaseinterface