SQLiteOpenHelper.getWritableDatabase问题

时间:2011-03-08 06:15:08

标签: java android sqlite

编辑:我相信我可能已经发现了这个问题。我需要处理我的SmartDBHelper类。将这样做并在此处发布结果。

编辑:找到有类似问题的人的链接。将检查这个以查看它是否尽快修复。 Another Post

编辑:更新以反映一些海报所提出的一些变化。问题仍然存在。

以下帖子:

我的应用程序需要能够从2个不同的事件写入android上的sqlite3数据库。我的一个事件是写入数据库就好了。当第二个事件尝试写入数据库时​​,会发生附加错误。我不知道为什么会发生这种情况或如何解决它。过去几个小时我尝试了很多东西,并用谷歌搜索了一下。有人可以查看下面的代码,让我知道你的想法吗?

如果您需要更多信息,请尽快告诉我。

提前致谢!

//This is the sqliteopenhelper i created
public class SmartDBHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "smart_lite_db.db";
    private static final int DATABASE_VERSION = 2;
    private static final String NOTIFY_TABLE_NAME = "user_notify_data";
    private static final String HR_TABLE_NAME = "user_hr_data";
    private static final String NOTIFY_TABLE_CREATE = 
        "CREATE TABLE " + NOTIFY_TABLE_NAME + 
        " (counter INTEGER PRIMARY KEY, " + 
        "userresponse INTEGER, " + 
        "notifytime INTEGER);";
    private static final String DATA_TABLE_CREATE = 
        "CREATE TABLE " + HR_TABLE_NAME +
        " (counter INTEGER PRIMARY KEY, " +
        "hr INTEGER, " +
        "act INTEGER, " +
        "timestamp INTEGER);";      

    public SmartDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        Log.v("smartdbhelper", "before creation");
        db.execSQL(NOTIFY_TABLE_CREATE);
        Log.v("smartdbhelper", "middle creation");
        db.execSQL(DATA_TABLE_CREATE);
        Log.v("smartdbhelper", "after creation");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}  

//This is the part that is working CORRECTLY
public class DataNotificationSurvey extends Activity {
    private SmartDBHelper dBHelper;
    private Date timeStamp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datanotificationlayout);
        Log.v("datanotificationsurvey", "inside datanotificationsurvey");

            dBHelper = new SmartDBHelper(this);
        timeStamp = new Date(DataNotification.when);
        // XML code stuff left out here, was not needed


    }

    public void submitNotify(int tempType, Date tempDate) {
        SQLiteDatabase dBH = dBHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("userresponse", tempType);
        values.put("notifytime", (tempDate.getTime()/1000));
        dBH.insert("user_notify_data", null, values);
        dBH.close();
    }
}  

// This is the event that is NOT working correctly
public class DataBuilder extends Activity {
    private List _listeners = new ArrayList();
    private SmartDataObject data;
    Context tThis;
    private SmartDBHelper dBHelper;
    private Date timeStampReference; //for keeping track of the first time

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v("databuilder", "on create");
    dBHelper = new SmartDBHelper(this);
}

    public void prepareData(SmartDataObject temp) {
        submitData(temp);
    }

    public void submitData(SmartDataObject temp) {
        data = temp;
        System.out.println("Where: DB-submitData");
        try {
        SQLiteDatabase dBH = dBHelper.getWritableDatabase(); // CODE FAILS AT THIS POINT
        Log.v("databuilder", "after writable");
        ContentValues values = new ContentValues();
        values.put("hr", data.getHeartRate());
        values.put("act", data.getAct());
        values.put("timestamp", data.getTimeStamp());
        dBH.insert("user_hr_data", null, values);
        Log.v("databuilder", "after insert");
        dBH.close();
        fireDataBuilderEvent(data);
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        catch(NullPointerException e) {
            e.printStackTrace();
        }
    }
    public synchronized void addDataBuilderListener(DataBuilderListener listener) {
        _listeners.add(listener);
    }
    public synchronized void removeDataBuilderListener(DataBuilderListener listener) {
        _listeners.remove(listener);
    }
    private synchronized void fireDataBuilderEvent(SmartDataObject temp) {
        DataBuilderEvent dRE = new DataBuilderEvent(this, temp);
        Iterator listeners = _listeners.iterator();
        while(listeners.hasNext()) {
            ((DataBuilderListener)listeners.next()).dataBuilderReceived(dRE);
        }
    }
    public interface DataBuilderListener {
        public void dataBuilderReceived(DataBuilderEvent event);
    }
}   

// The error that is occuring.
03-13 17:38:40.130: INFO/System.out(279): Where: DB-submitData
03-13 17:38:40.130: WARN/System.err(279): java.lang.NullPointerException
03-13 17:38:40.151: WARN/System.err(279):     at cpe495.smartapp.DataBuilder.submitData(DataBuilder.java:41)
03-13 17:38:40.151: WARN/System.err(279):     at cpe495.smartapp.DataBuilder.prepareData(DataBuilder.java:34)
03-13 17:38:40.171: WARN/System.err(279):     at cpe495.smartapp.SmartApp$2.dataAnalyzedReceived(SmartApp.java:56)
03-13 17:38:40.171: WARN/System.err(279):     at cpe495.smartapp.DataRobot.fireDataAnalyzedEvent(DataRobot.java:269)
03-13 17:38:40.181: WARN/System.err(279):     at cpe495.smartapp.DataRobot.analyzeData(DataRobot.java:79)
03-13 17:38:40.181: WARN/System.err(279):     at cpe495.smartapp.SmartApp$1.dataReceivedReceived(SmartApp.java:49)
03-13 17:38:40.191: WARN/System.err(279):     at cpe495.smartapp.ConnectDevice.fireDataReceivedEvent(ConnectDevice.java:79)
03-13 17:38:40.201: WARN/System.err(279):     at cpe495.smartapp.ConnectDevice.run(ConnectDevice.java:46)
03-13 17:38:40.211: WARN/System.err(279):     at java.lang.Thread.run(Thread.java:1096)

3 个答案:

答案 0 :(得分:2)

快速猜测:将private SmartDBHelper dBHelper = new SmartDBHelper(this);更改为private SmartDBHelper dBHelper;并仅在onCreate()中初始化新对象。

答案 1 :(得分:0)

使用SQLiteDatabase的方法检查数据库状态

isDbLockedByCurrentThread()
isDbLockedByOtherThreads

我认为它是由另一个线程锁定的。 或者在Androidmanifest.xml中检查您的提供商设置。

答案 2 :(得分:0)

我有这个问题,虽然我通常不会告诉任何人故意泄漏任何东西,但这是我能想到的唯一稳定的快速修复。

这为我解决了这个问题;

private static YourSQLiteOpenHelper helperAnchor;

并在onCreate

helperAnchor = this;

所以基本上给对象一个自己的引用来保持它的活着,因为在我的情况下,由于从ViewPager中的大量类调用我的本地数据库,所以没有别的地方可以理解它。这带来了额外的缺点,即必须创建另一个方法再次将其设置为null,您必须在需要清理它的地方调用它。

但是,嘿,它只是在颤抖。该应用程序现在是超级稳定的,之前它会在随机时间因SQLiteOpenHelper崩溃而崩溃。请告诉我是否/何时有更好的方法,因为坦率地说我不喜欢使用它。