构建SQLite数据库以分离可读/可写数据

时间:2017-11-11 09:11:49

标签: android database sqlite

我正在编写一个应用程序,允许用户阅读存储在SQLite数据库中的短篇小说。

到目前为止一切顺利。

但现在我想添加涉及写入数据库的功能(保存ScrollView的Y位置,以便用户可以从中断的地方继续,为故事添加书签等)。

我应该将这些值添加到books表中,还是应该使用user_settings(int),id(int)等列创建单独的表story_idy_position(int),bookmarked(布尔值)?

注意:我也在考虑将来在非本地数据库中存储故事的可能性。

我的另一个问题是:我是否需要将数据库移动到某处才能写入?我正在使用SQLiteAssetHelper,数据库当前位于/assets/databases/database.db。我听到一些关于/data/data/mypackage文件夹的说法,但我在项目中看不到它。

我的数据库设置目前如下:

authors
    id
    name
    name_alphanumeric

books
    id
    title
    author_id
    collection
    body

如果它有用,到目前为止这是我的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);
        // setForcedUpgrade();
    }

    // Getting all books
    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;
    }


    // Getting all stories
    public List<Book> getAllStories(int authorID) {

        List<Book> storyList = new ArrayList<>();

        // Select all query
        String selectQuery = "SELECT id, title FROM " + BOOKS + " WHERE author_id = " + authorID;

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

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Book book = new Book();
                book.setStoryID(Integer.parseInt(cursor.getString(0)));
                book.setTitle(cursor.getString(1));
                storyList.add(book);
            } while (cursor.moveToNext());
        }

        // return contact list
        return storyList;
    }

    // Get all collections
    public List<Book> getAllCollections(int authorID) {

        List<Book> collectionsList = new ArrayList<>();

        // Select all query
        String selectQuery = "SELECT DISTINCT collection FROM " + BOOKS + " WHERE author_id = " + authorID;
        Log.i("stories", selectQuery);

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

        // looping through all rows and adding to list

        if (cursor.moveToFirst()) {
            do {
                Book book = new Book();
                book.setCollection(cursor.getString(0));
                // Log.i("stories", cursor.getString(0));
                collectionsList.add(book);
            } while (cursor.moveToNext());
        }

        return collectionsList;
        // not sure how to log collectionsList here
    }

    // Get story

    public String getStoryBody(int storyID) {

        // Log.i("stories", Integer.toString(storyID));

        String storyBody = "";
        // String storyBody();

        // Select all query
        String selectQuery = "SELECT body FROM " + BOOKS + " WHERE id = " + storyID;

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

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

        return storyBody;
    }

    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 0;

    }

    public int getScrollPosition(int storyID) {

        int scrollPosition = 0;

        String selectQuery = "SELECT scroll_position FROM " + BOOKS + " WHERE id = " + storyID;

        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;

    }

}

1 个答案:

答案 0 :(得分:0)

  

但现在我想添加涉及写入数据库的功能   (保存ScrollView的Y位置,以便用户可以在哪里拿到   他们离开了,给故事加书签等等。

     

我应该将这些值添加到books表中,还是应该创建一个   使用id(int),story_id等列分隔表user_settings   (int),y_position(int),bookmarked(boolean)?

我认为您已明确表示它们是 USER 值,因此单独的用户表很可能是更易于管理的解决方案。

  

我的另一个问题是:我是否需要将数据库移动到某个地方   能写到吗?我正在使用SQLiteAssetHelper而数据库是   目前位于/assets/databases/database.db。我听到一些关于a的谈话   / data / data / mypackage文件夹,但我在项目中看不到它。

在所有可能性中,数据库已从资产文件夹复制到data/data/yourpackage/databases/dbfilename <{1>}的 SQLiteAssetHelper ,据我所知,这主要是它的用途。但是我'从来没有使用它。)这样的文件夹具有有限的访问权限(通常只有应用程序(root设备例外)),这很可能是你无法看到它的原因。

因此,写入/更新数据库的权限可能没有任何必要。