package com.app.util; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class AppDbAdapter { private static final String DATABASE_NAME = "app.db"; private static final String DATABASE_TABLE_COURSES = "courses"; private static final int DATABASE_VERSION = 1; public static final String KEY_ID = "_id"; public static final String KEY_COURSE_CID = "cid"; public static final String KEY_COURSE_CNAME = "cname"; public static final String KEY_COURSE_COUNT = "cdcount"; private static final String DATABASE_CREATE_COURSE = "create table " + DATABASE_TABLE_COURSES + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_COURSE_CID + " integer, " + KEY_COURSE_CNAME + " text, " + KEY_COURSE_COUNT + " integer);"; // Variable to hold the database instance private SQLiteDatabase db; // Context of the application using the database. private final Context context; // Database open/upgrade helper private DBHelper dbHelper; public AppDbAdapter(Context _context) { context = _context; dbHelper = new DBHelper(context, DATABASE_NAME, null, DATABASE_VERSION); } public AppDbAdapter open() throws SQLException { db = dbHelper.getWritableDatabase(); return this; } public void close() { db.close(); } public synchronized Cursor getAllCourses() { Cursor cursor = null; try { final String SQL_STATEMENT = "SELECT * FROM " + DATABASE_TABLE_COURSES + " order by " + KEY_COURSE_CID; cursor = db.rawQuery(SQL_STATEMENT, null); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return cursor; } /* public long insertEntry(Document objDoc) { ContentValues contentValues = new ContentValues(); try { contentValues.put(KEY_COURSE_CID, objDoc.cid); contentValues.put(KEY_DOCUMENT_DID, objDoc.id); contentValues.put(KEY_DOCUMENT_PATH, objDoc.filepath); contentValues.put(KEY_DOCUMENT_IMAGE_PATH, objDoc.imagePath); contentValues.put(KEY_DOCUMENT_DATE, objDoc.date); contentValues.put(KEY_DOCUMENT_NAME, objDoc.name); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return db.insert(DATABASE_TABLE_DOCS, null, contentValues); }*/ public void truncateCourseTable() { try { db.execSQL("DROP TABLE " + DATABASE_TABLE_COURSES); db.execSQL(DATABASE_CREATE_COURSE); } catch (Exception ce) { ce.printStackTrace(); } } /* public void truncateDocsTable() { try { db.execSQL("DROP TABLE " + DATABASE_TABLE_DOCS); db.execSQL(DATABASE_CREATE_DOCS); } catch (Exception ce) { ce.printStackTrace(); } }*/ private static class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } // Called when no database exists in disk and the helper class needs to // create a new one @Override public void onCreate(SQLiteDatabase _db) { try { _db.execSQL(DATABASE_CREATE_COURSE); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } } }
Saturday, 19 September 2015
Basic DataBase for Android
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment