我应该如何打开和正确地关闭我的数据库 [英] how should I open and close my database properly

查看:136
本文介绍了我应该如何打开和正确地关闭我的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它存储在一个SQLite DB.Also一些数据,我做了很多的查询我app.I一个重新查询有大约15活动it.And almoust都使用数据库来查询数据。 但是我在做什么是开放我的数据库中每一个活动,并关闭它的onDestroy {...}每个活动。

问题是,的onDestroy {...}可能永远不会被调用,有时我的应用程序停留在很长一段时间,我从一个活动切换到另一个开口,很多次我的数据库。

有时候我得到的错误,如数据库开过很多次,从来没有休息。

我会尝试关闭我的数据库完全后,我从它那里得到我的数据,并关闭它...移动到我的下一个活动,并重新开放等等..... 但问题是,在一些活动我来自其他活动回来....关闭我的DB和回来的活动会产生很大的力关闭。

我想要做的就是打开我的数据库,在我的应用程序的开始和关闭它在它的结束,但我现在面临两个问题:

1。 我应该让我的SQLiteOpenHelper类单身?......得到it..open它的一个实例在我的第一个活动,然后在我的下列活动只是让我的数据库是已经打开/ ???实例

2.Where是我的应用程序的结束????我怎么知道哪里是我的应用程序的结束,在哪里结束我的数据库。

编辑:

 公共类DBAdapter扩展SQLiteOpenHelper {

    公共DBAdapter(上下文的背景下){
        超级(上下文,DATABASE_NAME,空,1);
        this.myContext =背景;

    }


    公共无效的openDatabase()抛出的SQLException {




        字符串mypath中= DATABASE_PATH + DATABASE_NAME;
        DB = SQLiteDatabase.openDatabase(mypath中,空,
                SQLiteDatabase.OPEN_READWRITE);


    }

}
 

这是一块code从我的类来管理我的DB.To使这个单我应该使用构造likw这样的:

 私人DBAdapter()
{



//这里没有什么

}
 

但是,这是未定义SQLiteOpenHelper

编辑决赛: 这就是我符合zirael建议做到了:

  

包com.Server_1;

     

进口android.app.Application;

     

公共类MyApplication的扩展应用{

 私有静态DBAdapter分贝;

公共无效的onCreate()
{

    DB =新DBAdapter(getApplicationContext());
    db.createDatabase();
    db.openDataBase();
}

公共静态DBAdapter getDatabaseAdapter()
{
    返回分贝;
}


}
 


在每一次活动我在哪里,我需要DB连接我这样做:

  

MyApplication的所有MyApplication =(所有MyApplication)this.getApplication();

     

DBAdapter DB = myApplication.getDatabaseAdapter();

最后我的表现是这样的:

 <应用机器人:图标=@可绘制/图标
机器人:标签=@字符串/ APP_NAME
  机器人:MyApplication的NAME =
  机器人:可调试=真正的>
 

解决方案

在我的应用我打开所有MyApplication类到数据库的连接(可扩展应用程序自定义类 - 它应该被命名为相同androidManifest您的应用程序)<。 / P>

AndroidManifest.xml中

 &lt;应用机器人:标签=@字符串/ APP_NAME
    机器人:名称=com.mypackage.MyApplication机器人:可调试=真正的&GT;
 

所有MyApplication的.java

 公共类MyApplication的扩展应用{

    私人DatabaseAdapter dbAdapter;


    @覆盖
    公共无效的onCreate(){
        dbAdapter =新DatabaseAdapter(getApplicationContext());
        dbAdapter.open();
    }
 

和在需要DB连接我简单地使用每一个类:

  MyApplication的所有MyApplication =(所有MyApplication)this.getApplication();
DatabaseAdapter dbAdapter = myApplication.getDatabaseAdapter();
 

MyApplication的自动对每一个应用程序启动运行。这样,我只保留一个连接到数据库,所以它关闭,当应用程序被从内存中删除没有任何问题。

I have an app which stores some data in a SQLite DB.Also I'm doing a lot of query an requery in my app.I have about 15 activities in it.And almoust all use the DB to query for data. But what I'm doing is opening my DB in every activity and close it in onDestroy{...} of every activity.

The problem is that onDestroy{...} may never get called and sometimes my app stays on for a long time and I switch from an activity to another opening for to many times my DB.

And sometimes I get errors like-database too many times opened and never closed.

I would try to close my DB exactly after I get my data from it and close it...moving to my next activity and re-opening and so on..... But the problem is that in some activities I come back from other activities....closing my DB and coming back to that activity would produce a big Force Close.

What I wanna do is open my DB at the beginning of my app and close it at the end of it, but I'm facing 2 problems:

1. Should I make my SQLiteOpenHelper class a singleton?...get an instance of it..open it in my first activity and then in my following activities just get an instance of my DB which is already opened/???

2.Where is the end of my app????How should I know where is the end of my app and where to close my DB.

EDIT:

public class DBAdapter extends SQLiteOpenHelper {

    public DBAdapter(Context context) {
        super(context, DATABASE_NAME, null, 1);
        this.myContext = context;

    }


    public void openDataBase() throws SQLException {




        String myPath = DATABASE_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);


    }

}

That is a piece of code from my class that manages my DB.To make this singleton I should use a constructor likw this:

private DBAdapter()
{



//nothing in here

}

But this is undefined for SQLiteOpenHelper

EDIT FINAL: This is how I did it according with zirael advice:

package com.Server_1;

import android.app.Application;

public class MyApplication extends Application{

private static DBAdapter db;

public void onCreate()
{

    db=new DBAdapter(getApplicationContext());
    db.createDatabase();
    db.openDataBase();
}

public static DBAdapter getDatabaseAdapter()
{
    return db;
}


}


In every activity I where I need DB connection I do this:

MyApplication myApplication = (MyApplication) this.getApplication();

DBAdapter db= myApplication.getDatabaseAdapter();

And finally my manifest looks like:

<application  android:icon="@drawable/icon"  
android:label="@string/app_name"
  android:name=".MyApplication" 
  android:debuggable="true">

解决方案

In my app I open connection to database in myApplication class (your custom class that extends Application - it should be named the same as your application in androidManifest).

AndroidManifest.xml

<application android:label="@string/app_name" 
    android:name="com.mypackage.MyApplication " android:debuggable="true">

MyApplication .java

public class MyApplication extends Application {

    private DatabaseAdapter dbAdapter;


    @Override
    public void onCreate() {
        dbAdapter = new DatabaseAdapter(getApplicationContext());
        dbAdapter.open();
    }

And in each class that need db connection I simply use:

MyApplication myApplication = (MyApplication) this.getApplication();
DatabaseAdapter dbAdapter= myApplication.getDatabaseAdapter();

MyApplication is run automatically on every application start. This way I keep only one connection to DB so it's closed when app is being removed from memory without any problem.

这篇关于我应该如何打开和正确地关闭我的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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