问题描述
在 SQLite 数据库和共享首选项之间存储信息的好机制是什么?
What is the good mechanism to store information among SQLite database and Shared Preferences?
为什么要使用共享首选项?为什么要使用sqlite?我试图找到它们之间的区别,以及哪种数据存储机制更好,但我无法在 Google 上找到合适的答案.请帮我举例和解释.
Why use shared preferences? Why use sqlite? I tried to find the difference between them, and which is the better mechanism for data storing, but I am unable to find the appropriate answer on Google. Please help me with example and explanations.
推荐答案
这实际上取决于您要存储的数据.
It really depends on the data you want to store.
SQLite
大量相同的结构化数据应存储在 SQLite 数据库中,因为数据库是为此类数据设计的.由于数据是由数据库构建和管理的,因此可以使用 SQL 等查询语言进行查询以获取符合特定条件的数据子集.这使得在数据中搜索成为可能.当然,管理和搜索大量数据会影响性能,因此从数据库读取数据可能比从 SharedPreferences 读取数据慢.
Large amounts of same structured data should be stored in a SQLite database as databases are designed for this kind of data. As the data is structured and managed by the database, it can be queried to get a sub set of the data which matches certain criteria using a query language like SQL. This makes it possible to search in the data. Of course managing and searching large sets of data influences the performance so reading data from a database can be slower than reading data from SharedPreferences.
共享首选项
SharedPreferences 是一个键/值存储,您可以在其中保存某个键下的数据.要从商店读取数据,您必须知道数据的键.这使得读取数据非常容易.但是,存储少量数据很容易,存储和读取大型结构化数据也很困难,因为您需要为每个数据定义键,此外,除非您有一定的概念,否则您无法真正在数据中搜索命名键.
SharedPreferences is a key/value store where you can save a data under certain key. To read the data from the store you have to know the key of the data. This makes reading the data very easy. But as easy as it is to store a small amount of data as difficult it is to store and read large structured data as you need to define key for every single data, furthermore you cannot really search within the data except you have a certain concept for naming the keys.
这篇关于SQLite 和共享首选项的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!