Android SQLiteOpenHelper-每个表都有不同的类吗? [英] Android SQLiteOpenHelper - different class for every table?

查看:157
本文介绍了Android SQLiteOpenHelper-每个表都有不同的类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读本文( http://www.vogella.com/tutorials/AndroidSQLite/article.html )以了解android应用中的SQLite数据库.

I was reading this article (http://www.vogella.com/tutorials/AndroidSQLite/article.html) to learn about SQLite databases in android apps.

在文章中,他有一个提示:

In the article he has a tip:

优良作法是在每个表中创建一个单独的类.此类定义静态的onCreate()和onUpgrade()方法.这些方法在SQLiteOpenHelper的相应方法中调用.这样,即使您有多个表,您的SQLiteOpenHelper的实现也保持可读性.

It is good practice to create a separate class per table. This class defines static onCreate() and onUpgrade() methods. These methods are called in the corresponding methods of SQLiteOpenHelper. This way your implementation of SQLiteOpenHelper stays readable, even if you have several tables.

如果我正确理解了这个技巧,我应该为数据库中的每个表都提供一个类吗?

if I understand this tip correctly I should have a class for each table that I have in my database?

这真的是最佳做法吗?

如果是这样,那么使用多个表的复杂查询又如何呢?如果创建的类别不同,该如何处理?

If so, what about complex queries that uses multiple tables? how do I manage that if the creation is in different classes?

如何正确保留数据库版本?对于每个表更改,我都会更改数据库版本号吗?

How do I correctly keep the database version? for each table change I will change the database version number?

谢谢

推荐答案

SQLiteOpenHelper管理数据库文件,而不是表.您可以使用给定的数据库生命周期回调(onCreate()onUpgrade())自己管理表.

SQLiteOpenHelper manages database files, not tables. You manage the tables yourself with the given database lifecycle callbacks (onCreate(), onUpgrade()).

快速阅读可能会解释为作者主张为每个表创建一个单独的数据库帮助程序(我起初是这样做的),但事实并非如此.那真是个坏建议.

Quickly reading one could interpret that the author advocates creating a separate database helper for each table (I did at first), but that's not the case. That would have been bad advice.

重申作者的意图:

  • 一个数据库帮助程序类.
  • 该帮助程序涉及单独的表特定的帮助程序类,这些类不是SQLiteOpenHelper,只是为顶级数据库帮助程序做的一部分工作.
  • One database helper class.
  • The helper involves separate table-specific helper classes which are not SQLiteOpenHelpers but just doing part of the work for the top-level database helper.

这篇关于Android SQLiteOpenHelper-每个表都有不同的类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆