Back to project page book.
The source code is released under:
MIT License
If you think the Android project book listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com; /*w w w . jav a 2 s. com*/ import java.util.List; import fragments.PagerActivity; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import book.Book; /* * * * craopy db system for the app * * * book: * id : int * name : string * * * this could be normalized more, so each word can be in multiple books and vice versa * word * id : int * book_id : int * word : string * * translations * id : int * word : string * translation : string * */ public class Db extends SQLiteOpenHelper { private static final String DATABASE_NAME = "learnfin.db"; private static final int DATABASE_VERSION = 1; // it was said that it is good practice to create own class for each of the tables... // so at least one good practice here private BookTable books; // table book private WordTable words; // table word private PagerActivity pager; // main activity private TranslationsTable translations; // table for translations private SQLiteDatabase db; // the database // constructor public Db(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); db = this.getWritableDatabase(); // init pager = (PagerActivity) context; books = new BookTable(db); // per table classes for querying words = new WordTable(db); translations = new TranslationsTable(db); } // get book by id public Book getBook(long id){ Book bk = books.getBook( String.valueOf(id) ); words.get(bk); translations.get(bk); return bk; } // check if db contains a translation public boolean hasTranslations(String word){ return this.translations.hasWord(word); } // get translation from db for word public List<String> getTranslations(String word){ return this.translations.getTranslation(word); } /* * * * get contents for one book */ public Book getContent(Book bk){ words.get(bk); translations.get(bk); return bk; } public void removeWord(long id, String word){ this.words.removeWord(id, word); } /* * loading all the books at once because * i dont give a shit */ public List<Book> load(){ List<Book> bks = this.books.get(); if (bks.size() > 0 && bks != null){ for (Book book : bks){ book.setDb(this); words.get(book); translations.get(book); } } return bks; } // DELETEEEEEEE public void remove(Book book){ books.remove(book); } // save / update the books public void save(Book book){ books.save(book); words.save(book); translations.save(book); } // kill all public void clear(){ books.clear(); words.clear(); translations.clear(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { onCreate(db); } }