Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DatabaseClass extends SQLiteOpenHelper {
- public static final String DATABASE_NAME = "unipi_login1.db";
- public static final int DATABASE_VERSION = 1;
- public SQLiteDatabase db;
- private final Context context;
- public DatabaseClass(Context context) {
- // context: use to open or create the database
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- this.context = context;
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE credentials (USERNAME TEXT PRIMARY KEY, PASSWORD TEXT NOT NULL)");
- Log.w("DB", "credentials created");
- db.execSQL("CREATE TABLE news ( title TEXT PRIMARY KEY, " +
- " date TEXT NOT NULL , content_preview TEXT NOT NULL)");
- Log.w("DB", "news created");
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w("DB", "Upgrading from version " + oldVersion + " to " + newVersion);
- db.execSQL("DROP TABLE IF EXISTS credentials");
- db.execSQL("DROP TABLE IF EXISTS news");
- onCreate(db);
- }
- public DatabaseClass open() throws SQLException {
- Log.w("DB", "Database opened");
- db = this.getWritableDatabase();
- return this;
- }
- public void close() {
- Log.w("DB", "Database closed");
- db.close();
- }
- public void insertEntry(String username, String password) {
- ContentValues newValues = new ContentValues();
- newValues.put("USERNAME", username);
- newValues.put("PASSWORD", password);
- Log.w("DB", newValues + " inserted");
- db.insertWithOnConflict("credentials", null, newValues, SQLiteDatabase.CONFLICT_IGNORE);
- }
- public String getSingleEntry(String username) {
- Cursor cursor = db.query("credentials", null, " USERNAME=?", new String[] {username}, null, null, null);
- if (cursor.getCount() < 1) {
- cursor.close();
- return "NOT EXIST";
- }
- cursor.moveToFirst();
- String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
- cursor.close();
- return password;
- }
- public ArrayList<News> getAllNews(){
- ArrayList<News> newslist = new ArrayList<News>();
- // String[] newsColumns = { "title", "date", "text"};
- // grab all the information
- Cursor cursor = db.query("news", null, null, null, null, null, null);
- // loop through
- for (cursor.moveToLast(); !cursor.isBeforeFirst(); cursor.moveToPrevious()){
- News news = cursorToNews(cursor);
- newslist.add(news);
- }
- cursor.close();
- return newslist;
- }
- private News cursorToNews(Cursor cursor){
- News.Img img = News.Img.e2;
- News news = new News(cursor.getString(0), cursor.getString(1), cursor.getString(2), img);
- return news;
- }
- public SQLiteDatabase getDatabaseInstance() {
- return db;
- }
- public void insertNews(String title, String date, String content) {
- ContentValues newValues = new ContentValues();
- newValues.put("NEWS_TITLE", title);
- newValues.put("NEWS_DATE", date);
- newValues.put("NEWS_TITLE", content);
- Log.w("DB", newValues + " inserted");
- db.insertWithOnConflict("news", null, newValues, SQLiteDatabase.CONFLICT_IGNORE);
- }
- /* public int deleteEntry(String username) {
- Log.w("DB", username + " deleted");
- return db.delete(TABLE_NAME, "USERNAME=?", new String[] {username});
- }*/
- /* public void updateEntry(String username, String password) {
- Log.w("DB", "Entry Updated");
- ContentValues updatedValues = new ContentValues();
- updatedValues.put("USERNAME", username);
- updatedValues.put("PASSWORD", password);
- db.update(TABLE_NAME, updatedValues, "USERNAME=?", new String[] { username });
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement