Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.alex.lab5_database;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import java.util.ArrayList;
- import java.util.List;
- public class db extends SQLiteOpenHelper {
- // private static final String DATABASE_NAME="classmates";
- // private static final String TABLE_CLASS="Classmates";
- //
- // public void onCreate(SQLiteDatabase db){
- // db.execSQL("CREATE TABLE " + TABLE_CLASS + "(_id INTEGER PRIMARY KEY
- // AUTOINCREMENT ," + COL NAME + " TEXT ,"+ COL PHONE + " TEXT);");
- // }
- // @Override
- // public void onUpgrade(SQLiteDatabase db, intoldVersion, intnewVersion){
- // db.execSQL("DROP TABLE IF EXISTS"+ TABLE_CLASS);
- // onCreate(db);
- // }
- // Cursor query(String table, String[] columns, String selection,
- // String[] selectionArgs, String groupBy, String having, String sortOrder);
- public static final int DATABASE_VERSION = 1;
- public static final String DATABASE_NAME = "classmates.db";
- public static final String TABLE_CLASS = "Classmates";
- public static final String COLUMN_ID = "_id";
- public static final String COLUMN_NAME = "full name";
- public static final String COLUMN_TIME = "time registered";
- // public db(Context context) {
- // super(context, DATABASE_NAME, null, DATABASE_VERSION);
- // }
- public db(Context context) {
- super(context, TABLE_CLASS, null, 1);
- }
- // Database creation sql statement
- @Override
- public void onCreate(SQLiteDatabase database) {
- String DATABASE_CREATE = "create table "
- + TABLE_CLASS + " (" + COLUMN_ID
- + " integer primary key autoincrement, " + COLUMN_NAME
- + " text, "+ COLUMN_TIME + " long)";
- database.execSQL(DATABASE_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // Log.w(com.example.alex.lab5_database.db.class.getName(),
- // "Upgrading database from version " + oldVersion + " to "
- // + newVersion + ", which will destroy all old data");
- // db.execSQL("DROP TABLE IF EXISTS " + TABLE_CLASS);
- // onCreate(db);
- db.execSQL("DROP IF TABLE EXISTS " + TABLE_CLASS);
- onCreate(db);
- }
- // Adding a new classmate
- boolean addClassmate(String name, long time) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(COLUMN_NAME, name); // Classmate Name
- values.put(COLUMN_TIME,time);
- // Inserting Row
- long result = db.insert(TABLE_CLASS, null, values);
- // db.close();
- if (result == -1) {
- return false;
- } else {
- return true;
- }
- }
- // Getting single classmate
- Classmate getClassmate(int id) {
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.query(TABLE_CLASS, new String[] { COLUMN_ID,
- COLUMN_NAME, COLUMN_TIME }, COLUMN_ID + "=?",
- new String[] { String.valueOf(id) }, null, null, null, null);
- if (cursor != null)
- cursor.moveToFirst();
- Classmate classmate = new Classmate(Integer.parseInt(cursor.getString(0)),
- cursor.getString(1), new java.sql.Time(cursor.getLong(2)));
- // return classmate
- return classmate;
- }
- // Getting All Classmates
- public List<Classmate> getAllClassmates() {
- List<Classmate> classmateList = new ArrayList<Classmate>();
- // Select All Query
- String selectQuery = "SELECT * FROM " + TABLE_CLASS;
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- Classmate classmate = new Classmate();
- classmate.setId(Integer.parseInt(cursor.getString(0)));
- classmate.setName(cursor.getString(1));
- classmate.setTime(new java.sql.Time(cursor.getLong(2)));
- // Adding contact to list
- classmateList.add(classmate);
- } while (cursor.moveToNext());
- }
- // return contact list
- return classmateList;
- }
- // Updating single classmate
- public int updateClassmate(Classmate classmate) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(COLUMN_NAME, classmate.getName());
- // updating row
- return db.update(TABLE_CLASS, values, COLUMN_ID + " = ?",
- new String[] { String.valueOf(classmate.getId())});
- }
- // Deleting single contact
- public void deleteContact(Classmate classmate) {
- SQLiteDatabase db = this.getWritableDatabase();
- db.delete(TABLE_CLASS, COLUMN_ID + " = ?",
- new String[] { String.valueOf(classmate.getId()) });
- db.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement