2017-11-11 215 views
1

我正在編寫一個應用程序,允許用戶閱讀存儲在SQLite數據庫中的短篇小說。構建SQLite數據庫以分離可讀/寫數據

到目前爲止這麼好。

但是現在我想添加一些涉及寫入數據庫的功能(保存ScrollView的Y位置,以便用戶可以選擇它們離開的位置,爲書籤添加故事等)。

我應該將這些值添加到books表,或者我應該創建一個單獨的表user_settings的列像id(INT),story_id(INT),y_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; 

    } 

} 

回答

0

但現在我想補充一點,涉及寫入功能數據庫 (保存ScrollView的Y位置,以便用戶可以從 中刪除,爲書籤添加故事等)。

我應該將這些值添加到books表,或者我應該創建一個像ID(int)的列 單獨的表user_settings,story_id (INT),y_position(INT),書籤(布爾)?

我想你已經明確表示,他們是USER值,因此它很可能是一個單獨的用戶表將是更好的更易於管理的解決方案。

我的另一個問題是:我需要將數據庫移動到 能寫入它嗎?我正在使用SQLiteAssetHelper,目前數據庫爲 ,位於/assets/databases/database.db。我聽到一些關於 /data/data/mypackage文件夾的討論,但我在我的項目中看不到它。

在所有likeliehood數據庫已被複制從資產文件夾到data/data/yourpackage/databases/dbfilenameSQLiteAssetHelper據我所知這主要就是它的功能。不過,我從來沒有使用過。)這樣的文件夾訪問有限(通常只有應用程序(紮根設備是一個例外)),所以很可能是爲什麼你看不到它。

因此,寫入/更新數據庫的權限方式可能沒有任何要求。