无法使用SQLite Asset Helper写入预加载的数据库

时间:2017-11-22 12:47:52

标签: android sqlite

我正在使用SQLite Asset Helper来访问预加载的数据库。我可以成功读取它(getAllAuthors()方法按预期工作),但我在写入时遇到了麻烦。

Log.i("insert", insertQuery)Log.i("select", selectQuery)按预期返回,执行insertQueryselectQuery时没有错误。但是当我在SQLite应用程序的外部数据库浏览器中打开我的数据库时,数据库没有任何变化。

DatabaseHelper

public class DatabaseHelper extends SQLiteAssetHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "database9.db";
    private static final String BOOKS = "books";
    private static final String AUTHORS = "authors";

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

    // THIS WORKS FINE
    public ArrayList<Author> getAllAuthors() {

        ArrayList<Author> authorList = new ArrayList<>();

        // Select all query
        String selectQuery = "SELECT id, name FROM " + AUTHORS + " ORDER BY name_alphabetic";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                // create new author object
                Author author = new Author();
                // set ID and name of author object
                author.setID(Integer.parseInt(cursor.getString(0)));
                author.setName(cursor.getString(1));
                // pass author object to authorList array
                authorList.add(author);
            } while (cursor.moveToNext());
        }

        // return author list
        return authorList;
    }

    public int setScrollPosition(int scrollY, int storyID) {

        String insertQuery = "UPDATE " + BOOKS + " SET scroll_position = " + scrollY + " WHERE id = " + storyID;
        Log.i("insert", insertQuery);
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(insertQuery);

        return scrollY;

    }

    public int getScrollPosition(int storyID) {

        int scrollPosition = 0;

        String selectQuery = "SELECT scroll_position FROM " + BOOKS + " WHERE id = " + storyID;
        Log.i("select", selectQuery);

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                scrollPosition = cursor.getInt(0);
            } while (cursor.moveToNext());
        }

        return scrollPosition;

    }

}

StoryBodyActivity

public class StoryBodyActivity extends AppCompatActivity {

    private TextView storyBodyTextView;
    private ScrollView storyBodyScrollView;
    public int storyID;
    Parcelable state;
    int scrollY;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_story_body, menu);
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story_body);

        Bundle extras = getIntent().getExtras();

        String story = extras.getString("story");
        storyID = extras.getInt("story_id");
        Log.i("stories", Integer.toString(storyID));

        storyBodyTextView = (TextView) findViewById(R.id.story_body_text_view);
        storyBodyScrollView = (ScrollView) findViewById(R.id.story_body_scroll_view);

        DatabaseHelper db = new DatabaseHelper(this);
        scrollY = db.getScrollPosition(storyID);
        Log.i("scrolly", Integer.toString(scrollY));

        storyBodyScrollView.scrollTo(0, scrollY);

        String storyBody = db.getStoryBody(storyID);

        storyBodyTextView.setText(Html.fromHtml(storyBody));

        if(state != null) {
            Log.d("pause", "trying to restore textview state..");
            storyBodyTextView.onRestoreInstanceState(state);
        }

        int scroll = storyBodyScrollView.getScrollY();
        Log.i("onCreate scroll", Integer.toString(scroll));

    }

    @Override
    public void onPause() {
        // Log.d("pause", "saving listview state @ onPause");
        // state = storyBodyTextView.onSaveInstanceState();
        scrollY = storyBodyScrollView.getScrollY();
        Log.i("onPause scroll", Integer.toString(scrollY));
        // Log.i("insert", Integer.toString(storyID));
        DatabaseHelper db = new DatabaseHelper(this);
        db.setScrollPosition(scrollY, storyID);
        super.onPause();
    }

    @Override
    public void onResume(){
        super.onResume();
        Log.i("onResume scroll", Integer.toString(scrollY));
        storyBodyScrollView.scrollTo(0, scrollY);

    }
}

解决

结合使用下面接受的答案,解决我的ScrollView问题(解决方案here)。

1 个答案:

答案 0 :(得分:0)

资产是只写/受保护的,如果您需要更新数据库,则必须确保将其复制到可以更新的位置。例如默认的/ data / data / your_package / databases /文件夹。

SQLiteAssethelper将简单地执行此操作。

  

但是当我在SQLite应用程序的外部数据库浏览器中打开我的数据库时,   没有任何改变数据库。

您希望setScrollPosition插入新行还是更新现有行?按照编码,它会做后者。

使用execSQL不是推荐的执行更新的方法,而SQLiteDatabase update方法的优点是它返回执行的更新次数。因此,我建议改变: -

public int setScrollPosition(int scrollY, int storyID) {

    String insertQuery = "UPDATE " + BOOKS + " SET scroll_position = " + scrollY + " WHERE id = " + storyID;
    Log.i("insert", insertQuery);
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(insertQuery);

    return scrollY;

}

来: -

public int setScrollPosition(int scrollY, int storyID) {
    Contentvalues cv = new Contentvalues();
    cv.put("scroll_position", scrolly);
    String whereclasue = "id=?";
    String[] whereargs = new String[]{storyID}; 

    SQLiteDatabase db = this.getWritableDatabase();
    int updates = db.update(BOOKS,cv,whereclause,whereargs);
    if (updates > 0) {
        Log.i("update","Updated " + Integer.toString(updates) + " rows.";
    } else {
        log.i("update","No Updates performed");
    }
    return scrollY;
}

您可能希望更改方法以返回更新的行数。

如果您使用上述内容并在日志中获得 No Updates ,则可能会突出显示数据库无法更改的原因。

尽管如此,更新的数据库不是资产文件夹中的数据库,而是位于data/data/your_package/databases/database9.db的数据库

您可能希望查看this。这包括允许您检查数据库和表的方法。

相关问题