无法在Android应用程序中打开数据库

时间:2011-11-16 11:47:54

标签: android database sqlite

在我的应用程序中我试图从数据库中获取一些数据,我在列表中显示它。当我在摩托罗拉(2.1)和三星Ace(2.3)以及我的一些模拟器中运行应用程序时,它们工作正常。

但是当我在三星Galaxy Tab-P7500(3.1)中运行时,它显示以下错误

 sqlite3_open_v2("/data/data/com.pack.store/databases/chaek.db", &handle, 6, NULL) failed
    E/SQLiteDatabase(10021): Failed to open the database. closing it.
    E/SQLiteDatabase(10021): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
    E/SQLiteDatabase(10021): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:750)
    E/SQLiteDatabase(10021): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:223)
    E/SQLiteDatabase(10021): at com.pack.sp.SPTocPage.getTOC(SPTocPage.java:246)
    E/SQLiteDatabase(10021): at com.pack.sp.SPTocPage.onCreate(SPTocPage.java:82)
    E/SQLiteDatabase(10021): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
    E/SQLiteDatabase(10021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
    E/SQLiteDatabase(10021): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
    E/SQLiteDatabase(10021): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
    E/SQLiteDatabase(10021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
    E/SQLiteDatabase(10021): at android.os.Handler.dispatchMessage(Handler.java:99)
    E/SQLiteDatabase(10021): at android.os.Looper.loop(Looper.java:132)
    E/SQLiteDatabase(10021): at android.app.ActivityThread.main(ActivityThread.java:4028)
    E/SQLiteDatabase(10021): at java.lang.reflect.Method.invokeNative(Native Method)
    E/SQLiteDatabase(10021): at java.lang.reflect.Method.invoke(Method.java:491)
    E/SQLiteDatabase(10021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
    E/SQLiteDatabase(10021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    E/SQLiteDatabase(10021): at dalvik.system.NativeStart.main(Native Method)
    E/SQLiteOpenHelper(10021): Couldn't open chaek.db for writing (will try read-only):
    E/SQLiteOpenHelper(10021): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
    E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
    E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
    E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
    E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
    E/SQLiteOpenHelper(10021): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:750)
    E/SQLiteOpenHelper(10021): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
    E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
    E/SQLiteOpenHelper(10021): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:223)
    E/SQLiteOpenHelper(10021): at com.pack.sp.SPTocPage.getTOC(SPTocPage.java:246)
    E/SQLiteOpenHelper(10021): at com.pack.sp.SPTocPage.onCreate(SPTocPage.java:82)
    E/SQLiteOpenHelper(10021): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
    E/SQLiteOpenHelper(10021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
    E/SQLiteOpenHelper(10021): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
    E/SQLiteOpenHelper(10021): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
    E/SQLiteOpenHelper(10021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
    E/SQLiteOpenHelper(10021): at android.os.Handler.dispatchMessage(Handler.java:99)
    E/SQLiteOpenHelper(10021): at android.os.Looper.loop(Looper.java:132)
    E/SQLiteOpenHelper(10021): at android.app.ActivityThread.main(ActivityThread.java:4028)
    E/SQLiteOpenHelper(10021): at java.lang.reflect.Method.invokeNative(Native Method)
    E/SQLiteOpenHelper(10021): at java.lang.reflect.Method.invoke(Method.java:491)
    E/SQLiteOpenHelper(10021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
    E/SQLiteOpenHelper(10021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    E/SQLiteOpenHelper(10021): at dalvik.system.NativeStart.main(Native Method)
    E/SqliteDatabaseCpp(10021): sqlite3_open_v2("/data/data/com.pack.sp/databases/chaek.db", &handle, 1, NULL) failed
    E/SQLiteDatabase(10021): Failed to open the database. closing it.
    E/SQLiteDatabase(10021): android.database.sqlite.SQLiteDatabaseLockedException: database is locked
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:983)
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
    E/SQLiteDatabase(10021): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:233)
    E/SQLiteDatabase(10021): at com.pack.sp.SPTocPage.getTOC(SPTocPage.java:246)
    E/SQLiteDatabase(10021): at com.pack.sp.SPTocPage.onCreate(SPTocPage.java:82)
    E/SQLiteDatabase(10021): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
    E/SQLiteDatabase(10021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1715)
    E/SQLiteDatabase(10021): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1767)
    E/SQLiteDatabase(10021): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
    E/SQLiteDatabase(10021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1005)
    E/SQLiteDatabase(10021): at android.os.Handler.dispatchMessage(Handler.java:99)
    E/SQLiteDatabase(10021): at android.os.Looper.loop(Looper.java:132)
    E/SQLiteDatabase(10021): at android.app.ActivityThread.main(ActivityThread.java:4028)
    E/SQLiteDatabase(10021): at java.lang.reflect.Method.invokeNative(Native Method)
    E/SQLiteDatabase(10021): at java.lang.reflect.Method.invoke(Method.java:491)
    E/SQLiteDatabase(10021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
    E/SQLiteDatabase(10021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
    E/SQLiteDatabase(10021): at dalvik.system.NativeStart.main(Native Method)
    E/android.os.Debug(  284): Dumpstate > /data/log/dumpstate_app_error

以下是我的Db类文件

public class ChaekHelper extends SQLiteOpenHelper 
{
    private static final int DATABASE_VERSION = 7;
    private static final String DATABASE_NAME = "chaek.db";
    private List<Map<String, String>> ALL_TABLES = new ArrayList<Map<String, String>>();
    public ChaekHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.onSetCreateQuery(context);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        for (int i = 0; i < ALL_TABLES.size(); i++) 
        {
            db.execSQL("CREATE TABLE IF NOT EXISTS " + ALL_TABLES.get(i).get("TABLE_NAME") + " ( "+ ALL_TABLES.get(i).get("FIELD_NAME") + " );");
        }

        // create Index table
        db.execSQL("DROP TABLE IF EXISTS dual;");
        db.execSQL("CREATE TABLE dual (idx INTEGER PRIMARY KEY, rownum TEXT);");
        db.execSQL("CREATE UNIQUE INDEX idx_rownum ON dual (rownum);");

        // create all tables
        db.beginTransaction();
        try 
        {
            for (int i = 1; i < 100; i++) {
                String idxStr = "0" + i;
                if (i > 9)
                    idxStr = idxStr.substring(1);

                db.execSQL("INSERT INTO dual (idx, rownum) VALUES (" + i + ",'" + idxStr + "');");
            }
            db.setTransactionSuccessful();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            db.endTransaction();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        for (int i = 0; i < ALL_TABLES.size(); i++) {
            db.execSQL("DROP TABLE IF EXISTS " + ALL_TABLES.get(i).get("TABLE_NAME") + ";");
        }
        onCreate(db);
    }

    private void onSetCreateQuery(Context context) {
        Map<String, String> params;

        // 1. BOOK
        params = new HashMap<String, String>();
        params.put("TABLE_NAME", context.getResources().getString(R.string.BOOK_TBL_NAME));
        params.put("FIELD_NAME", context.getResources().getString(R.string.BOOK_TBL_FIELDS));
        ALL_TABLES.add(params);

        // 2. TOC
        params = new HashMap<String, String>();
        params.put("TABLE_NAME", context.getResources().getString(R.string.TOC_TBL_NAME));
        params.put("FIELD_NAME", context.getResources().getString(R.string.TOC_TBL_FIELDS));
        ALL_TABLES.add(params);

        // 3. BOOKMARK
        params = new HashMap<String, String>();
        params.put("TABLE_NAME", context.getResources().getString(R.string.BM_TBL_NAME));
        params.put("FIELD_NAME", context.getResources().getString(R.string.BM_TBL_FIELDS));
        ALL_TABLES.add(params);
    }
}

为什么会这样?任何人都能解释一下这个......我已经浪费了三天......

2 个答案:

答案 0 :(得分:1)

public class BaseDAO extends OrmLiteSqliteOpenHelper {

public static Dao<?, Integer> instanseDao(Context cont, Class c,String Dbname) {
   File path=new File(DATABASE_PATH);
   File f = new File(DATABASE_PATH+DB_NAME);//改默认数据库目录需先手动创建数据库文件
if (!path.exists()) {
      path.mkdirs();// 创建一个目录
}
if (!f.exists()) {
      try {
          f.createNewFile();//创建文件
      } catch (IOException e) {
          e.printStackTrace();
      }
    }




//how do??????

    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f,null);//创建数据库实例
    BaseDAO baseDAO = new BaseDAO(cont);
    //baseDAO.onCreate(db); 
    //baseDAO.onUpgrade(db,baseDAO.getConnectionSource(), OLD_DB_VERSION, DB_VERSION);
    //baseDAO.close();//注意要先关掉
    //db =SQLiteDatabase.openDatabase(DATABASE_PATH+DB_NAME, null, SQLiteDatabase.CREATE_IF_NECESSARY);

    try {
        Dao = baseDAO.getDao(c);
    } catch (SQLException e) {
        e.printStackTrace();
    }

    CONT=cont;
    C=c;
    DB_NAME = Dbname;
    return Dao;
}

答案 1 :(得分:0)

你试试这段代码

try {
        db = this.openOrCreateDatabase("chaek.db", MODE_PRIVATE, null);
        db.close();
        openDatabase();
        createTable();
    } catch (SQLException se) {
        Toast.makeText(this, se.getMessage(), 1).show();
    }
private void openDatabase() {
    try {
db = SQLiteDatabase.openDatabase("/data/data/com.pack.store/databases/chaek.db",
                null, SQLiteDatabase.CREATE_IF_NECESSARY);
        //Toast.makeText(this, "DB was opened!", 1).show();
    } catch (SQLiteException e) {
        Toast.makeText(this, e.getMessage(), 1).show();
    }
}// createDatabase