将列表存储到数据库并一起检索它们:Android
我想存储一个字符串列表,然后检索它们。我将如何使用数据库来做到这一点?
I want to store a list of strings, and then retrieve them. How would I do this using a database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理想情况下,通过结构化方式来解决此问题,让您的主要活动/服务与实用程序类进行交互,该实用程序类直接与数据库而不是活动/服务本身进行通信。
通过这样做,您可以模块化流程,使其比在一个类中完成所有操作更容易处理。具体来说,流程将遵循此
“活动/服务”->“活动/服务”。数据库实用程序类->数据库
该活动通过实用程序类获取数据库结果。您可以在实用程序类中拥有所有数据库功能,例如添加、减去、编辑记录。
具体来说,您需要研究 SQLiteOpenHelper
需要提到的一件事是首先在纸上创建表,并设置所需的关系和列名称,然后构建这样的语句:
然后在创建了您的表之后表,然后只需获取数据库的句柄,如下所示:
此行将返回数据库的链接,您可以在其中根据需要查询、删除、更新!
Ideally the way to approach this from a structured way to have your main activity/service interact with a Utility class that directly communicates with the database instead of the activity/service it self.
By doing this you modularize the process making its a lot easier to handle than doing it all in one class. Specifically the flow would follow this
Activity/Service -> Database Utility Class -> Database
The activity gets database results through the utility class. You could have all your database functionality like adding, subtracting, editing of records in the utility class.
Specifically you need to look into SQLiteOpenHelper
One thing to mention is first create your tables on paper, and setup the relations and column names that you want and then build a statement like this:
Then after you have created your table, then just grab a handle to your database like so:
This line will return you a link to your database, where you can query, delete, update as necessary!