“assets”文件夹中的文件在Android中不是只读的

时间:2018-04-02 00:00:28

标签: android android-studio sqlite android-sqlite

我正在开发一个Android应用程序,我在应用程序的“assets”文件夹中提供了一个SQLite数据库文件和APK。现在他们说,资产文件是只读的,但在我的情况下,我可以在运行时修改它,比如改变几列的值。只读数据库文件是否会忽略更改值?

void update()
{
    sqLiteDatabase = databaseManager.getWritableDatabase();
    sqLiteDatabase.execSQL("UPDATE quotes SET likes = '"+ 1 + "' WHERE id ="+count);
    sqLiteDatabase.execSQL("UPDATE quotes SET dislikes = '"+ 1 + "' WHERE id ="+count);
    sqLiteDatabase.execSQL("UPDATE quotes SET like_status = '"+ 1 + "' WHERE id ="+count);
}


public class DatabaseManager extends SQLiteAssetHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "local_database.db";

    private static final String TABLE_NAME = "post";
    private static final String COLUMN_1 = "id";
    private static final String COLUMN_2 = "content";
    private static final String COLUMN_3 = "likes";
    private static final String COLUMN_4 = "dislikes";
    private static final String COLUMN_5 = "like_status";

    DatabaseManager(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int old_version, int new_version) {

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

    }

    int get_count()
    {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+ TABLE_NAME, null);
        Integer count = cursor.getCount();
        cursor.close();

        return count;
    }

    Cursor get_like_status(Integer ID)
    {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+ TABLE_NAME + " WHERE id = '" + ID + "';", null);
        cursor.moveToFirst();

        return cursor;
    }

    Cursor read_data(Integer ID)
    {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+ TABLE_NAME + " WHERE id = '" + ID + "';", null);
        cursor.moveToFirst();

        return cursor;
    }

    boolean update_counts(Integer count, Long likes, Long dislikes) {

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_3, likes);
        contentValues.put(COLUMN_4, dislikes);

        return sqLiteDatabase.update(TABLE_NAME, contentValues, "id = " + count, null) != -1;
    }
}

1 个答案:

答案 0 :(得分:1)

您正在使用的库(我假设android-sqlite-asset-helper从资产文件夹中复制数据库,如the documentation所示:

  

将从资产中提取数据库并将其复制到应用程序的私人数据目录中。