Sql Lite崩溃问题

时间:2016-01-09 04:58:50

标签: android sqlite android-sqlite

在SQL Lite上第一次尝试。我创建了一个数据库,几乎没有问题可以在示例测验中实现。现在这里是问题。 1.完成所有问题后,它会抛出调试错误,因为close()缺失。 2.如果我实现close(),应用程序崩溃 3.无论close()如何,db创建都不会更新并显示旧数据,需要卸载并重新安装app才能再次运行。

这是我的数据库帮助程序类文件中的代码。

    package ccccc
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "QuizOnline";
    // tasks table name
    private static final String TABLE_QUEST = "quest";
    // tasks Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_QUES = "question";
    private static final String KEY_ANSWER = "answer"; //correct option
    private static final String KEY_OPTA= "opta"; //option a
    private static final String KEY_OPTB= "optb"; //option b
    private static final String KEY_OPTC= "optc"; //option c
    private static final String KEY_OPTD= "optd"; //option d

    private SQLiteDatabase dbase;
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        dbase=db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
                + " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
                +KEY_OPTB +" TEXT, "+KEY_OPTC +" TEXT, "+KEY_OPTD+" TEXT)";
        db.execSQL(sql);        
        addQuestions();

    }
    private void addQuestions()
    {
        Question q1=new Question("Highest mountain peak in the world"+"?","Everest","K2","T3","P3","Everest");
        this.addQuestion(q1);
//...........>omited rest questions/entries added
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
        // Create tables again
        onCreate(db);
    }
    // Adding new question
    public void addQuestion(Question quest) {
        //SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_QUES, quest.getQUESTION()); 
        values.put(KEY_ANSWER, quest.getANSWER());
        values.put(KEY_OPTA, quest.getOPTA());
        values.put(KEY_OPTB, quest.getOPTB());
        values.put(KEY_OPTC, quest.getOPTC());
        values.put(KEY_OPTD, quest.getOPTD());
        // Inserting Row
        dbase.insert(TABLE_QUEST, null, values);
        //dbase.close();-----> close given here
    }
    public List<Question> getAllQuestions() {
        List<Question> quesList = new ArrayList<Question>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        dbase=this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Question quest = new Question();
                quest.setID(cursor.getInt(0));
                quest.setQUESTION(cursor.getString(1));
                quest.setANSWER(cursor.getString(2));
                quest.setOPTA(cursor.getString(3));
                quest.setOPTB(cursor.getString(4));
                quest.setOPTC(cursor.getString(5));
                quest.setOPTD(cursor.getString(6));
                quesList.add(quest);
            } while (cursor.moveToNext());
        }
        // return quest list
        return quesList;
    }
    public int rowcount()
    {
        int row=0;
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        row=cursor.getCount();
        return row;
    }
}

日志:

01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase: close() was never explicitly called on database '/data/data/com.xxx.quiz/databases/QuizOnline' 
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase: android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:787)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at com.xxx.quiz.DbHelper.getAllQuestions(DbHelper.java:267)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at com.xxx.quiz.QuizActivity.onCreate(QuizActivity.java:43)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.app.Activity.performCreate(Activity.java:4492)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.os.Looper.loop(Looper.java:137)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at java.lang.reflect.Method.invoke(Method.java:511)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-09 10:16:16.038 19636-19645/com.xxx.quiz E/SQLiteDatabase:     at dalvik.system.NativeStart.main(Native Method)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System: Uncaught exception thrown by finalizer
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System: java.lang.IllegalStateException: Don't have database lock!
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at android.util.LruCache.trimToSize(LruCache.java:197)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at android.util.LruCache.evictAll(LruCache.java:285)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
01-09 10:16:16.048 19636-19645/com.xxx.quiz E/System:     at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:1)

您不得保留提供给onCreate()onUpdate()的数据库副本。

对于实际的数据访问:你可以

  • 在应用开始时拨打getWritableDatabase()一次,并在某处保存引用(正如您对dbase所做的那样)或
  • 每次都会调用getWritableDatabase(),并在您完成后调用close(),然后保持参考。

    在这种情况下,为了确保对象始终关闭,您应该使用try / finally

    SQLiteDatabase db = getWritableDatabase();
    try {
        ... use db ...
    } finally {
        db.close();
    }
    

如果你混合使用这两种方法,你很可能会经常或过少地致电close()

游标总是需要close() d。