在Android的Samsung y duos设备中创建SQLite DB失败了?

时间:2014-01-20 07:14:00

标签: android sqlite

我有android应用程序。它工作的大多数设备,但它只在Samsung y duos中不起作用。

我的应用程序数据库包含6个表,2个表数据已加密。没有这种加密,它工作正常,只有加密数据后我才遇到这个问题。

错误是:

    01-20 07:03:27.791: I/SqliteDatabaseCpp(13747): sqlite returned: error code = 11, msg = database corruption at line 48188 of [ed759d5a9e], db=/data/data/com_group_mylibs/databases/mylibs_db
01-20 07:03:27.791: I/SqliteDatabaseCpp(13747): sqlite returned: error code = 11, msg = database disk image is malformed, db=/data/data/com_group_mylibs/databases/mylibs_db
01-20 07:03:27.791: I/SqliteDatabaseCpp(13747): sqlite returned: error code = 11, msg = database corruption at line 48188 of [ed759d5a9e], db=/data/data/com_group_mylibs/databases/mylibs_db
01-20 07:03:27.791: I/SqliteDatabaseCpp(13747): sqlite returned: error code = 11, msg = database disk image is malformed, db=/data/data/com_group_mylibs/databases/mylibs_db
01-20 07:03:27.791: E/SqliteDatabaseCpp(13747): sqlite3_exec - Failed to set synchronous mode = 1(Normal) 
01-20 07:03:27.791: I/SqliteDatabaseCpp(13747): sqlite returned: error code = 11, msg = database corruption at line 48188 of [ed759d5a9e], db=/data/data/com_group_mylibs/databases/mylibs_db
01-20 07:03:27.791: I/SqliteDatabaseCpp(13747): sqlite returned: error code = 11, msg = database disk image is malformed, db=/data/data/com_group_mylibs/databases/mylibs_db
01-20 07:03:27.791: E/SqliteDatabaseCpp(13747): CREATE TABLE android_metadata failed
01-20 07:03:27.791: E/DefaultDatabaseErrorHandler(13747): Corruption reported by sqlite on database: /data/data/com.group.mylibs/databases/mylibs.db
01-20 07:03:27.801: E/DefaultDatabaseErrorHandler(13747): deleting the database file: /data/data/com.group.mylibs/databases/mylibs.db
01-20 07:03:28.001: I/SqliteDatabaseCpp(13747): sqlite returned: error code = 1, msg = no such table: hint, db=/data/data/com_group.mylibs/databases/mylibs_db
01-20 07:03:28.001: W/dalvikvm(13747): threadid=1: thread exiting with uncaught exception (group=0x40c31a68)
01-20 07:03:28.011: E/AndroidRuntime(13747): FATAL EXCEPTION: main
01-20 07:03:28.011: E/AndroidRuntime(13747): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.group.mylibs/com.group.mylibs.LibraryActivity}: android.database.sqlite.SQLiteException: no such table: hint: , while compiling: SELECT id, wordTypeId, hintText FROM hint WHERE wordTypeId= 0

这是我的代码:

public class MyLibsDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mylibs.db";
private static final int DATABASE_VERSION = 5;
private static String DB_PATH = "/data/data/com.group.mylibs/databases/";
private static String DB_NAME = "mylibs.db";
private final Context myContext;
private SQLiteDatabase mylibsDataBase;

public SQLiteDatabase getMylibsDataBase() {
    return mylibsDataBase;
}

public void setMylibsDataBase(SQLiteDatabase mylibsDataBase) {
    this.mylibsDataBase = mylibsDataBase;
}



public MyLibsDatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    myContext = context;
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    try {
        createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onCreate(SQLiteDatabase database) {
}



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();
    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {

            throw new Error("Error copying database");
        }
    }
}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time open the application.
 * 
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;
    try
    {
    File dbFile = new File(DB_PATH + DB_NAME); 
    return dbFile.exists();
} catch (Exception ex) {
    Log.e("Error checkDataBase", ex.toString());
    return false;
}
}

private void copyDataBase() throws IOException {
    try {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
    } catch (Exception ex) {
        Log.e("Error copyDataBase", ex.toString());
    }
}

public void openDataBase() throws SQLException {
    // Open the database
    String myPath = DB_PATH + DB_NAME;

    this.mylibsDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
}

此应用程序适用于大多数模拟器&设备,仅在Samsung y duos中出现问题。

我检查了sqlite版本3.7&我创建的数据库版本是3.6.18。

请告诉我什么是问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

查看Android源代码,似乎是DefaultDatabaseErrorHandler.java课程 表示您的数据库已损坏。查看DefaultDatabaseErrorHandler.java和您的日志表明正在调用onCorruption()类的DefaultDatabaseErrorHandler.java,该方法会进一步删除您的数据库。因此,您为加密添加的2个表可能会导致问题。我建议你检查这两个表是否会对整个数据库造成任何损坏。