暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

在Android应用程序中使用Db4o

原创 eternity 2022-07-27
325

Db4o是一个对象数据库,即忘记关系数据库模型中表的映射。如果您是一名开发人员,这就意味着节省了在应用程序中投入的时间和代码量。Db4o的巨大潜力在于,您可以通过多次保存和检索[plain,non-mapped]对象来重用它们。您可以使用嵌套集合或层次结构中任何复杂级别的其他复杂对象来持久化复杂对象或设计模式。您不局限于使用基本类型的平面对象来获得合理的性能。

使用db4o,您还可以从本机查询中受益,本机查询使您更接近该语言,并将优化的执行与简单性结合起来,即使对于复杂的查询也是如此。Db4o是开源的,可以在这里下载。

为了在应用程序中使用db4o,只需在项目中引用一个jar文件即可:无需安装、无需服务器,也无需复杂的安装。对于使用Eclipse的Java项目,只需将jar文件放到“lib”文件夹中,并将库添加到构建路径中。根据需要,您必须选择db4o xxx yyy。要包括的jar文件。如果“xxx”是“all”,则可以获得db4o的所有功能。如果它是“核心”,则得到最小值。关于“yyy”,取决于您需要支持的Java版本(例如,db4o-xx-core-java5.jar针对Java JDK 5和JDK 6)。

如何在Android中使用db4o

我非常喜欢在Android上使用db4o。我通常首先创建一个中心类(例如Db4oHelper),该类处理db4oapi并控制何时创建、打开和关闭数据库。

public class Db4oHelper {
    private static ObjectContainer oc = null;
    private Context context;
    /**            * @param ctx     */
    public Db4oHelper(Context ctx) {
        context = ctx;
    }
    /**    * Create, open and close the database    */
    public ObjectContainer db() {
        try {
            if (oc == null || oc.ext().isClosed()) {
                oc = Db4oEmbedded.openFile(dbConfig(), db4oDBFullPath(context));
                // We first load the initial data from the database
                ExercisesLoader.load(context, oc);
            }
return oc;
        } catch (Exception ie) {
            Log.e(Db4oHelper.class.getName(), ie.toString());
            return null;
        }
    } /**    * Configure the behavior of the database    */
    private EmbeddedConfiguration dbConfig() throws IOException {
        EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();
        configuration.common().objectClass(Exercise.class).objectField("name").indexed(true);
        configuration.common().objectClass(Exercise.class).cascadeOnUpdate(true);
        configuration.common().objectClass(Exercise.class).cascadeOnActivate(true);
        return configuration;
    } /**       * Returns the path for the database location       */
    private String db4oDBFullPath(Context ctx) {
        return ctx.getDir("data", 0) + "/" + "pumpup.db4o";
    } /**       * Closes the database       */
    public void close() {
        if (oc != null) oc.close();
    }
}
}

接下来,我们可以为每个类创建一个提供者,在这里我们可以实现相关的查询(例如,ExerciseProvider.java来处理练习的持久性)。我们可以让它扩展Db4o助手以获得所有维护方法(打开、关闭等)。

public class ExerciseProvider extends Db4oHelper {   
public final static String TAG = "ExerciseProvider"; 
private static ExerciseProvider provider = null;              public
ExerciseProvider(Context ctx) { 
super(ctx);              }              public static 
ExerciseProvider getInstance(Context ctx) {  
if (provider == null)
provider = new ExerciseProvider(ctx);
return provider;              }              public void
store(Exercise exercise) {
db().store(exercise); 
delete(Exercise exercise) { 
db().delete(exercise);
findAll() {                            return
db().query(Exercise.class);

在退出应用程序之前,不要忘记关闭数据库,这一点非常重要。否则,您的更改将无法提交。在Android中实现这一点的一个好方法是在启动器活动(应用程序的主要活动)中,我们将创建db4o Helper的实例,并在退出时调用close()。

public class main extends Activity { 
private Db4oHelper db4oHelper = null;              @Override 
public void onCreate(Bundle icicle) { 
super.onCreate(icicle);
setContentView(R.layout.main);                            dbHelper(); 
}            /**              * Create Db4oHelper instance
*/              private Db4oHelper dbHelper() {
if (db4oHelper == null) {
db4oHelper = new Db4oHelper(this);
db4oHelper.db();                            }                         
return db4oHelper;              }                         /**            
* Close database before exiting the application              */      
@Override              protected void onPause() {                       
super.onDestroy();                            dbHelper().close();     
db4oHelper = null;              }}

要旨

如果您不必使用内容交付工具,那么在Android上使用db4o是一个不错的选择。只有当您必须与其他外部应用程序共享数据并且与Android的SQLite过于耦合时,才需要使用这些工具。对于处理单个应用程序的所有持久性方面,使用db4o是一件令人愉快的事情:它不会带来麻烦,节省了大量时间,并且简单明了。

原文标题:Using Db4o in an Android Application
原文作者:Damasia Maneiro
原文链接:https://dzone.com/articles/using-db4o-android-application

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论