数据库被锁定android

时间:2014-08-04 06:14:52

标签: android android-sqlite

我已经使用数据库来存储一些信息。我已经为它创建了两个表。但是每当我使用数据库时,我都会收到一个错误,说数据库被锁定了。我不知道为什么会发生这种情况。即使我关闭了数据库。

数据库代码

public class GroupDataBase extends SQLiteOpenHelper {

    private static final int dbVersion = 1;
    private static final String dbName = "HSsuraksha";
    private static final String grouptableName = "groupDetails";
    private static final String contacttableName = "contactDetails";
    private static final String groupId = "groupId";
    private static final String groupName = "groupName";
    private static final String createdOn = "createdOn";
    private static final String contactId = "contactId";
    private static final String contactGroupId = "groupId";
    private static final String contactName = "contactName";
    private static final String contactNumber = "contactNumber";
    private String groupIdValue;
    private ArrayList<String> groupIdList;
    private ArrayList<GroupModel> getGroupInfo;
    private static final String createContactTable = "Create Table " + contacttableName + "(" + contactId + " Integer Primary Key AutoIncrement," + contactGroupId + " Text," + contactName + " Text," + contactNumber + " Text" + ");";
    private static final String createGroupTable = "CREATE TABLE " + grouptableName + "(" + groupId + " Integer Primary Key AutoIncrement," + groupName + " Text," + createdOn + " Text" + ");";


    public GroupDataBase(Context context) {
        super(context, dbName, null, dbVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        sqLiteDatabase.execSQL(createGroupTable);
        sqLiteDatabase.execSQL(createContactTable);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

//        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + contacttableName);
//        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + contacttableName);
//        onCreate(sqLiteDatabase);

    }


    public void insertGroupDetails(GroupModel groupModel) {
        SQLiteDatabase database = getWritableDatabase();
        database.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put(groupName, groupModel.getGroupName());
        contentValues.put(createdOn, groupModel.getGroupCreatedDate());
        if (contentValues != null) {

            Long id = database.insert(grouptableName, null, contentValues);
            Log.e("Group insert values", "" + id);

        }
        database.setTransactionSuccessful();
        database.endTransaction();
        database.close();

    }

    public ArrayList<String> getGroupId() {
        SQLiteDatabase database = getReadableDatabase();
        String selectID = "Select " + groupId + " From " + grouptableName;
        Cursor c = database.rawQuery(selectID, null);
        groupIdList = new ArrayList<String>();

        while (c.moveToNext()) {
            groupIdValue = c.getString(c.getColumnIndex(groupId));
            groupIdList.add(groupIdValue);
        }

        database.close();

        return groupIdList;
    }

    public ArrayList<GroupModel> getAllGroups() {
        SQLiteDatabase database = getWritableDatabase();
        String query = "Select * From " + grouptableName;
        GroupModel groupModel;
        getGroupInfo = new ArrayList<GroupModel>();
        Cursor c = database.rawQuery(query, null);
        while (c.moveToNext()) {

            groupModel = new GroupModel(c.getString(c.getColumnIndexOrThrow(groupId)), c.getString(c.getColumnIndexOrThrow(groupName)), c.getString(c.getColumnIndexOrThrow(createdOn)));
            getGroupInfo.add(groupModel);

        }
        c.close();
        database.close();
        return getGroupInfo;
    }

    public void insertContacts(ContactModel contactModel, String id, ArrayList<ContactModel> contactModelArrayList) {
        SQLiteDatabase database = getWritableDatabase();
        database.beginTransaction();
        ContentValues contentValues = new ContentValues();
        for (int i = 0; i < contactModelArrayList.size(); i++) {

            contentValues.put(contactName, contactModelArrayList.get(i).getContactName());
            contentValues.put(contactNumber, contactModelArrayList.get(i).getContactNumber());
            contentValues.put(groupId, id);
            if (contentValues != null) {
                Long value = database.insert(contacttableName, id, contentValues);
                Log.e("Insert Contact", "" + value);
            }

        }


        database.setTransactionSuccessful();
        database.close();
    }

    public void selectContacts(String id) {
        String query = "Select * From " + contacttableName + " where " + groupId + "=?";
        SQLiteDatabase database = getReadableDatabase();
        Cursor cursor = database.rawQuery(query, new String[]{id});
        while (cursor.moveToNext()) {
            String contactNameValue = cursor.getString(cursor.getColumnIndexOrThrow(contactName));
            Log.e("Contact Name", "" + contactNameValue);
        }
        cursor.close();
        database.close();

    }


}

我访问数据库的代码

case R.id.bt_group_create:
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                date = simpleDateFormat.format(new Date());
                groupModel = new GroupModel(strGroupName, date);
                groupDataBase.insertGroupDetails(groupModel);
                arrayListgroupId = groupDataBase.getGroupId();
                strGroupId = arrayListgroupId.get(arrayListgroupId.size() - 1);
                Log.e("Group Id", "" + strGroupId);

//                contactModel = new ContactModel(contactName, contactNumber);
                groupDataBase.insertContacts(new ContactModel(), strGroupId, contactModelArrayList);

                groupDataBase.selectContacts(strGroupId);


        }

logcat的

08-04 11:36:30.188  24330-24330/example.com.pocketdocs E/SQLiteLog﹕ (5) database is locked
08-04 11:36:32.718  24330-24330/example.com.pocketdocs E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
            at android.database.sqlite.SQLiteConnection.nativeExecuteForLong(Native Method)
            at android.database.sqlite.SQLiteConnection.executeForLong(SQLiteConnection.java:602)
            at android.database.sqlite.SQLiteSession.executeForLong(SQLiteSession.java:652)
            at android.database.sqlite.SQLiteStatement.simpleQueryForLong(SQLiteStatement.java:107)
            at android.database.DatabaseUtils.longForQuery(DatabaseUtils.java:816)
            at android.database.DatabaseUtils.longForQuery(DatabaseUtils.java:804)
            at android.database.sqlite.SQLiteDatabase.getVersion(SQLiteDatabase.java:870)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:242)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
            at example.com.pocketdocs.DataBase.GroupDataBase.selectContacts(GroupDataBase.java:131)
            at example.com.pocketdocs.Group.CreateNewGroup.onClick(CreateNewGroup.java:105)
            at android.view.View.performClick(View.java:4147)
            at android.view.View$PerformClick.run(View.java:17161)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

Logcat在此行指向错误

 SQLiteDatabase database = getReadableDatabase();

2 个答案:

答案 0 :(得分:0)

解决了问题。这只是我的错误

错误代码

public void insertContacts(ContactModel contactModel, String id, ArrayList<ContactModel> contactModelArrayList) {
        SQLiteDatabase database = getWritableDatabase();
        database.beginTransaction();
        ContentValues contentValues = new ContentValues();
        for (int i = 0; i < contactModelArrayList.size(); i++) {

            contentValues.put(contactName, contactModelArrayList.get(i).getContactName());
            contentValues.put(contactNumber, contactModelArrayList.get(i).getContactNumber());
            contentValues.put(groupId, id);
            if (contentValues != null) {
                Long value = database.insert(contacttableName, id, contentValues);
                Log.e("Insert Contact", "" + value);
            }

        }


        database.setTransactionSuccessful();
        database.close();
    }

校正: 我忘记调用endtransaction,所以我的db obj没有关闭

public void insertContacts(ContactModel contactModel, String id, ArrayList<ContactModel> contactModelArrayList) {
        database = getWritableDatabase();
        database.beginTransaction();

        try {
            ContentValues contentValues = new ContentValues();
            for (int i = 0; i < contactModelArrayList.size(); i++) {

                contentValues.put(contactName, contactModelArrayList.get(i).getContactName());
                contentValues.put(contactNumber, contactModelArrayList.get(i).getContactNumber());
                contentValues.put(groupId, id);
                if (contentValues != null) {
                    Long value = database.insert(contacttableName, id, contentValues);
                    Log.e("Insert Contact", "" + value);
                }

            }
        } catch (Exception e) {

        } finally {
            database.setTransactionSuccessful();
            database.endTransaction();
            database.close();
        }

答案 1 :(得分:-1)

当您尝试写入数据库时​​,可能是另一个写入数据库的线程。

有关详细信息,请参阅Android Database Locked