简体   繁体   English

存储和调用数据的最有效方法是什么?

[英]What is the most effective method of storing and recalling data?

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. 我一直想建立一个非常类似于Twitter的大型项目,我刚刚启动它,并且在创建帐户时遇到了一些错误。 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. 我知道我可以使用.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. 如果我可以在Python程序中使用数据库,那会容易得多,这意味着该程序可以仅作为一个文件运行,并且更容易编写代码。 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. 谢谢您的帮助。

Your expectations of Python are very low. 您对Python的期望非常低。 That is great news because you are going to be very happy. 这是个好消息,因为您会感到非常高兴。

You can definitely use a database with Python. 您绝对可以在Python中使用数据库。 The simplest and quickest to set up as well as closest to what you described is a file database: SQLite. 建立最简单,最快,最接近您所描述的是文件数据库: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 如果您想坚持比简单,可以使用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? 如果要构建Web应用程序,那么为什么要重新发明轮子呢? Choose one from the many Python web frameworks . 许多Python Web框架中选择一种。 Some if not all of them abstract away the database so you can focus on your application and its features. 其中一些(如果不是全部的话)将数据库抽象化,因此您可以专注于应用程序及其功能。

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. 这变得非常快,但是就处理python语法而言,这非常简单。 This answer handles that subject well: 这个答案很好地处理了这个问题:

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最安全的从数据库存储和检索密码的方法

Use SQLite or any other RDBMS like PostgreSQL. 使用SQLite或任何其他RDBMS(如PostgreSQL)。 To use Python with RDBMS: https://wiki.python.org/moin/DatabaseInterfaces 要将Python与RDBMS一起使用: https : //wiki.python.org/moin/DatabaseInterfaces

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM